I want to post a form with JSONP is there any feasible solution?
I want to post form to different domain from jsonp.
We can only use JSONP when: The API itself supports JSONP . It needs to return the JSON response wrapped in a function and it usually lets us pass in the function name we want it to use as one of the query params. We can only use it for GET requests, it doesn't work for PUT / POST / DELETE and so on.
JSONP is vulnerable to the data source replacing the innocuous function call with malicious code, which is why it has been superseded by cross-origin resource sharing (available since 2009) in modern applications.
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.
It works by dynamically adding a <script> tag to the DOM and calling a predefined function with the remote web service's JSON data as the parameter. The <script> tag is not subject to the same origin policy and therefore can request content cross-domain.
You cannot make a cross origin POST with JSONP.
However, you can:
A JSONP request is just creating a script tag with a function call:
Javascript:
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script src = 'url_to_post.serverside?callback=callback_function';
var callback_function(response) {
head.removeChild(script);
alert(response.abc); // returns def;
};
url_to_post.serverside:
callback_function({"abc": "def"});
Hope you can see why it only works for GET requests
:)
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