所以,我正在设置一个 RESTful API,一切似乎都很好。
它可以通过邮递员成功地向本地服务器发出 PUT 请求:
Status Code: 200 OK
Access-Control-Allow-Methods: GET, POST, PUT, LOGIN, OPTIONS
Access-Control-Allow-Origin: *
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Length: 299
Content-Type: application/json
Date: Sat, 14 Sep 2013 17:56:28 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=100
Pragma: no-cache
Server: Apache/2.2.25 (Win32) PHP/5.3.19
X-Powered-By: PHP/5.3.19
但是当使用带有以下代码的ajax时:
$.ajax({
url: SD.AJAX+'users?uname=jamie&pword=jamie',
dataType: 'json',
type: 'login',
data: {
'uname':'jamie',
'pword':'jamie'
},
crossDomain: true
});
我收到这些错误:
OPTIONS http://sexdiaires.local/users?uname=jamie&pword=jamie 405 (Method Not Allowed) jquery.js:8526
OPTIONS http://sexdiaires.local/users?uname=jamie&pword=jamie Method LOGIN is not allowed by Access-Control-Allow-Methods. jquery.js:8526
XMLHttpRequest cannot load http://sexdiaires.local/users?uname=jamie&pword=jamie. Method LOGIN is not allowed by Access-Control-Allow-Methods.
为什么,使用 ajax 会有什么不同,尽管我必须承认收到的标题由于某种原因是不同的:/
HTTP/1.1 405 Method Not Allowed
Date: Sat, 14 Sep 2013 18:00:38 GMT
Server: Apache/2.2.25 (Win32) PHP/5.3.19
X-Powered-By: PHP/5.3.19
Set-Cookie: PHPSESSID=36an511ifo3jsu6uh6a1fqoc82; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Access-Control-Allow-Origin: http://sd.local
Content-Length: 3562
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html