Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the jQuery ajax call support PATCH?

When I send this ajax rquest:

$.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.

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 
like image 305
Sam Avatar asked Nov 30 '12 09:11

Sam


People also ask

How do I send a patch request in AJAX?

var request = new XMLHttpRequest(); request. open('PATCH', 'http://127.0.0.1:8001/api/v1/pulse/6/', false); request. setRequestHeader("Content-type","application/json"); request. send('{"isActive": 1}');

Is jQuery support AJAX?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

What methods does AJAX support?

However, GET, POST, PUT and DELETE are supported by the implementations of XMLHttpRequest (i.e. AJAX calls) in all the major web browsers (IE, Firefox, Safari, Chrome, Opera). reference : Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

Which is better jQuery or AJAX?

jQuery uses ajax for many of its functions, but it nothing else than a library that provides easier functionality. With jQuery you dont have to think about creating xml objects ect ect, everything is done for you, but with straight up javascript ajax you need to program every single step of the ajax call.


1 Answers

The $.ajax method does support HTTP PATCH.

The problem you are seeing is that the ajax method looks for PATCH in the Access-Control-Allow-Methods response 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.

Here's an example using Java:

response.addHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE"); 
like image 170
Ray Toal Avatar answered Sep 20 '22 16:09

Ray Toal