ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – 404错误调用REST api(Phalcon)

2019-08-30 04:28:46  阅读:191  来源: 互联网

标签:phalcon php ajax apache http-headers


尝试使用ajax在Phalcon中调用DELETE或PUT时,我收到404错误.我能够做一个GET.

我的阿贾克斯:

$.ajax({
    url: 'http://localhost/person/blah/5',
    type: 'DELETE',
    success: function (data) {
        console.log(data);
    }
}); 

我的PHP

$app->delete('/person/blah/{id:[0-9]+}', function($id) {
    $response = new Phalcon\Http\Response();
    $response->setJsonContent(array('status' => 'OK', 'data' => array('id' => $id)));
    return $response;  
}); 

在此之前,我遇到了DELETE,PUT和GET的CORS问题.我更改了我的.htaccess文件以允许访问控制,并且GET开始工作.但是,我现在有404问题DELETE和PUT.

这是我的.htaccess文件

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$index.php?_url=/$1 [QSA,L]
</IfModule>

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "DELETE, PUT, GET"
    Header set Access-Control-Allow-Headers: "X-Requested-With, Content-Type"
</IfModule> 

我的猜测是这个问题与CORS有关. javascript正在我的计算机上运行(而非服务器).我的javascript在C:/Users/username/Desktop/test.html中.我曾在Firefox和Chrome上试过并遇到同样的问题.

一些额外的信息

响应标题

Access-Control-Allow-Head...    X-Requested-With, Content-Type
Access-Control-Allow-Meth...    DELETE, PUT, GET
Connection  Keep-Alive
Content-Length  15
Content-Type    text/html
Date    Tue, 10 Feb 2015 00:25:17 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
Status  404 Not Found
X-Powered-By    PHP/5.5.9
access-control-allow-orig...    *

请求标头

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding     gzip, deflate
Accept-Language     en-US,en;q=0.5
Access-Control-Request-Me...    DELETE
Connection  keep-alive
DNT     1
Host    localhost
Origin  null
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101     Firefox/35.0

缓存中的响应头与响应头相同

如果重要的话,我已经能够使用curl成功调用我的DELETE.

$curl -i -X DELETE http://localhost/person/blah/5
HTTP/1.1 200 OK
Date: Mon, 09 Feb 2015 23:18:40 GMT
Server: Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
X-Powered-By: PHP/5.5.9
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: DELETE, PUT, GET
Content-Length: 33
Content-Type: text/html

{"status":"OK","data":{"id":"5"}} 

解决方法:

我的.htaccess文件中没有我需要的所有东西.

我按照这个指南
http://benjaminhorn.io/code/setting-cors-cross-origin-resource-sharing-on-apache-with-correct-response-headers-allowing-everything-through/

我需要这个:

# Always set these headers.
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$$1 [R=200,L]

标签:phalcon,php,ajax,apache,http-headers
来源: https://codeday.me/bug/20190830/1765840.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有