【问题标题】:404 error when calling REST api (Phalcon)调用 REST api (Phalcon) 时出现 404 错误
【发布时间】:2015-04-10 02:11:07
【问题描述】:

我在尝试使用 ajax 在 Phalcon 中调用 DELETE 或 PUT 时收到 404 错误。我可以做一个 GET。

我的 ajax:

$.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 开始工作。但是,我现在遇到了 DELETE 和 PUT 的 404 问题。

这是我的 .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"}} 

【问题讨论】:

    标签: php ajax apache http-headers phalcon


    【解决方案1】:

    我的 .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]
    

    【讨论】:

    • 最好的方法是在代码中处理这个问题。但是这也很好。
    • 是的,但这很好用,因为 Chrome 在执行 POST 之前会执行 OPTIONS,并且由于 404 错误不会继续,我们不需要 OPTIONS 但我们不希望它停止在此,我们希望继续处理,在经过大量搜索后给您 1 分,这很好。
    【解决方案2】:

    可能希望按照流程图查看您遗漏的内容: http://www.html5rocks.com/static/images/cors_server_flowchart.png

    如果您正在利用 cookie 或需要公开响应标头,则需要打开

    访问控制公开标头

    访问控制允许凭据

    即使你不是,也可能想尝试使用它们。

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 2021-05-04
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多