因此,我正在设置一个RESTful应用程序接口,一切似乎都进行得很顺利。
可以通过postman成功向本地服务器发起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
发布于 2013-09-15 02:10:37
这是因为type: 'login'
不是一个有效的HTTP方法。
来自jQuery's doc
类型(默认为'GET')类型: String要发起的请求类型("POST“或"GET"),默认为"GET”。注意:这里也可以使用其他HTTP请求方法,例如PUT和DELETE,但并不是所有浏览器都支持这些方法。
你试过使用type: 'PUT'
吗?
https://stackoverflow.com/questions/18804810
复制相似问题