jQuery ajax调用是否支持PATCH?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (2292)

当我发送这个Ajax请求时:

$.ajax({
            headers : {
                'Accept' : 'application/json',
                'Content-Type' : 'application/json'
            },
            url : 'http://localhost:8080/wutup/venues/12',
            type : 'PATCH',
            data : JSON.stringify({description: "842490812321309213801923 gonzagazors"}),
            success : function(response, textStatus, jqXhr) {
                console.log("Venue Successfully Patched!");
            },
            error : function(jqXHR, textStatus, errorThrown) {
                // log the error to the console
                console.log("The following error occured: " + textStatus, errorThrown);
            },
            complete : function() {
                console.log("Venue Patch Ran");
            }
        });

我收到这个错误:

XXMLHttpRequest cannot load http ://localhost:8080/wutup/venues/12. Method PATCH is not allowed by Access-Control-Allow-Methods.

curl:

 $ curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PATCH -    d' {"address": "8421 Gonzaga Ave"}'  http://localhost:8080/wutup/venues/12 

About to connect() to localhost port 8080 (#0)
Trying 127.0.0.1... connected
Connected to localhost (127.0.0.1) port 8080 (#0)
PATCH /wutup/venues/12 HTTP/1.1
User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
Host: localhost:8080
Accept: application/json
Content-type: application/json
Content-Length: 57

HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Date: Fri, 30 Nov 2012 08:14:35 GMT

Connection #0 to host localhost left intact
Closing connection #0
提问于
用户回答回答于

$.ajax方法确实支持HTTP PATCH。

这是一个使用Java的例子:

response.addHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE");
用户回答回答于

难道你的浏览器不支持PATCH方法吗?

取自jQuery AJAX API文档

请求的类型(“POST”或“GET”),默认为“GET”。注意:其他的HTTP请求方法,比如PUT和DELETE,也可以在这里使用,但是并不是所有浏览器都支持。

扫码关注云+社区

领取腾讯云代金券