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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With