JSONP solves the problem of Cross-domain Ajax invocation. Why cross Domain Ajax calls? This can be in an application directly in front of the front-end through JS invoke another application (under different domain name) API.
We also use the JSONP in the actual application, but only then knew only one limit of JSONP, can only send GET request, the flaw of the request is the limitation of the length of requests.
Today, the discovery of another limitation of JSONP (in the jquery Ajax scenario)-does not trigger $.ajax's error callback, as follows:
Copy Code code as follows:
$.ajax ({
DataType: ' Jsonp ',
Error:function (XHR) {
This callback function is not executed when an error occurs
}
});
This restriction is determined by the JSONP implementation mechanism.
Workaround:
Using a jquery plugin--jquery-jsonp,https://github.com/jaubourg/jquery-jsonp
Sample code:
Copy Code code as follows:
<script src= "Https://raw.github.com/jaubourg/jquery-jsonp/master/src/jquery.jsonp.js" ></script>
Copy Code code as follows:
$.jsonp ({
URL: ',
Success:function (data) {
},
Error:function (Xoptions, Textstatus) {
Console.log (Textstatus);
}
});
When a JSONP request fails, such as a 404 error, the error callback function executes, and the output string "error".