Javascript jQuery ajax 调用是否支持 PATCH?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13642044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does the jQuery ajax call support PATCH?
提问by Sam
When I send this ajax rquest:
当我发送这个 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");
}
});
I receive this error:
我收到此错误:
XMLHttpRequest cannot load http ://localhost:8080/wutup/venues/12. Method PATCH is not allowed by Access-Control-Allow-Methods.
XMLHttpRequest 无法加载 http://localhost:8080/wutup/venues/12。Access-Control-Allow-Methods 不允许方法 PATCH。
However, using 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
回答by Ray Toal
The $.ajaxmethod doessupport HTTP PATCH.
该$.ajax方法确实支持 HTTP PATCH。
The problem you are seeing is that the ajaxmethod looks for PATCH in the Access-Control-Allow-Methodsresponse header of the options preflight check. Either this header is missing from your response, or the PATCH method was not included in the value of this header. In either case, the problem is in the server, not in your client-side code.
您看到的问题是该ajax方法Access-Control-Allow-Methods在选项preflight check的响应标头中查找 PATCH 。您的响应中缺少此标头,或者此标头的值中未包含 PATCH 方法。无论哪种情况,问题都出在服务器上,而不是出在客户端代码中。
Here's an example using Java:
下面是一个使用 Java 的例子:
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE");
回答by Stanley
Could it be your browser doesn't support the PATCH method?
可能是您的浏览器不支持 PATCH 方法?
Taken from jQuery AJAX API documentation:
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.
要发出的请求类型(“POST”或“GET”),默认为“GET”。注意:这里也可以使用其他 HTTP 请求方法,如 PUT 和 DELETE,但并非所有浏览器都支持。

