Can you make a HTTP PATCH request from Javascript?

2019-01-25 08:52发布

问题:

I am working with an API that requires me to make an HTTP PATCH request as part of the URI, is this possible to do from Javascript, my research is showing that I can only do POST, GET, DELETE, and PUT. Is PATCH allowed?

Thank you,

回答1:

I'm not sure what you exactly mean by a "PATCH" request, but it seems to be possible (at least in Firefox 6 and Chromium 12). According to the Mozilla source code, there is only a limitation of TRACE and TRACK requests.

A quick testcase:

<!-- test.html -->
<script>
var x=new XMLHttpRequest();
x.open("patch", "/");
x.send(null);
</script>

Any webserver can be used, but I choose for Python's SimpleHTTPServer module.

$ ls
test.html
$ python -m SimpleHTTPServer
localhost - - [21/Sep/2011 17:32:11] "GET /test.html HTTP/1.1" 200 -
localhost - - [21/Sep/2011 17:32:11] code 501, message Unsupported method ('patch')
localhost - - [21/Sep/2011 17:32:11] "patch / HTTP/1.1" 501 -

So, as long as the server supports the method, the request get's passed.



回答2:

As of some research the PATCH method seems to be new (march 2010 http://tools.ietf.org/html/rfc5789) so if you try to define PATCH on an XMLHttpRequest it may work, but only on very latest revisions of modern browsers. Don't have a supported browser list found, yet.