2015-02-10 53 views
3

我在使用ajax尝试调用Phalcon中的DELETE或PUT时收到404错误。我能够做一个GET。调用REST api(Phalcon)时出现404错误

我的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; 
}); 

在此之前,我是越来越CORS问题与DELETE,PUT和GET。我改变了我的.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 

响应标头是相同响应头

如果米atters我已经能够使用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"}} 

回答

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] 
+0

最好的办法是在代码来处理这个。但这项工作很好地为好。 – zzarbi 2015-05-13 00:53:32

+0

确实如此,但这样做很好,因为Chrome在执行POST之前会执行OPTIONS,并且由于404错误不会继续,所以我们不需要OPTIONS,但我们不希望它停在此处,我们想继续处理,给你1点后大量的搜索这工作得很好。 – 2016-02-02 11:59:25

相关问题