0

所以,我正在设置一个 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
4

1 回答 1

0

那是因为type: 'login'isin 不是有效的 HTTP 方法。

来自jQuery 的文档

type (default: 'GET') Type: String 要发出的请求的类型(“POST”或“GET”),默认为“GET”。注意:这里也可以使用其他 HTTP 请求方法,例如 PUT 和 DELETE,但并非所有浏览器都支持。

你试过使用type: 'PUT'吗?

于 2013-09-14T18:10:37.123 回答