![](/img/trans.png)
[英]Apache - Error 404 calling REST API /codeguard/users - PHP Laravel - CentOS 7
[英]404 error when calling REST api (Phalcon)
嘗試使用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文件中沒有我需要的所有東西。
我需要這個:
# 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]
可能想要按照流程圖查看您錯過的內容: http : //www.html5rocks.com/static/images/cors_server_flowchart.png
如果您正在利用cookie或需要暴露響應標頭,則需要打開
訪問控制展露報頭
和
訪問控制允許的憑據
即使你不是,也可以嘗試使用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.