Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a jsonp POST request that specifies contentType with jQuery?

I need to make a jsonp POST request with the content type 'application/json'. I can get the POST request to the server like this:

      jQuery.ajax({         type: 'POST',         url: url,         data: data,         success: success,         error: error,         async: true,         complete: complete,         timeout: TIMEOUT,         scriptCharset: 'UTF-8',         dataType: 'jsonp',         jsonp: '_jsonp',       }); 

But as soon as I add the line:contentType: "application/json" it starts sending it as an OPTIONS request rather than a POST.

How can I specify the content type and still submit the request as a POST?

like image 566
Marcus Avatar asked Oct 05 '10 00:10

Marcus


People also ask

What does JSONP use for request?

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

What is contentType in jquery AJAX?

contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; charset=UTF-8 , which is the default. dataType is what you're expecting back from the server: json , html , text , etc.

What is JSONP jquery?

JSONP (which stands for JSON with Padding) builds on this technique and provides us with a way to access the returned data. It does this by having the server return JSON data wrapped in a function call (the “padding”) which can then be interpreted by the browser.

What is dataType JSONP?

dataType: jsonp for cross-domain request, that means request to different domain and dataType: json for same domain-same origin request. Loads in a JSON block using JSONP. Adds an extra "? callback=?" to the end of your URL to specify the callback.


1 Answers

It is not possible to make a JSONP POST request.

JSONP works by creating a <script> tag that executes Javascript from a different domain; it is not possible to send a POST request using a <script> tag.

like image 162
SLaks Avatar answered Sep 19 '22 02:09

SLaks