JQuery: get JSON via ajax, but with POST instead of GET

I am using jQuery $ .ajax to make a request to a third-party server using JSONP. I specify the method as POST, but it still uses GET:

$.ajax({ type: "POST", dataType: "json", url: other_server + "/run?callback=?", data: { code: $(code).val() }, success: function(obj) { var res = obj.results; $(results).val(res); } }); 

Looking at the jQuery source, I see these two lines that seem to force all cross-domain GET requests, but I don't understand why this should be like this:

 if ( s.crossDomain ) { s.type = "GET"; 

Is it possible to do this with POST instead of GET? Why is jQuery forcing GET?

+6
source share
1 answer

JSON-P works by inserting a <script> element in a document, so it can only execute GET requests.

If you want to make a POST request to a remote server, you need to look at XHR and configure CORS permissions . Please note that this is limited browser support .

Alternatively, save your requests in the same source (and your server will make a request to the remote server).

+5
source

Source: https://habr.com/ru/post/897995/


All Articles