This article describes how to solve the restrictions when using jsonp in jqueryajax. You can refer to this method to solve cross-origin ajax calls. Why cross-origin ajax call? In this way, the API of another application (under different domain names) can be called directly at the frontend through js in an application.
We have also used jsonp in practical applications, but we only know one restriction of jsonp before and can only send get requests. The disadvantage of get requests is that the request length is limited.
Today, we found another restriction of jsonp (in the case of jquery ajax) -- the error callback of $. ajax will not be triggered. The sample code is as follows:
The Code is as follows:
$. Ajax ({
DataType: 'jsonp ',
Error: function (xhr ){
// The callback function is not executed when an error occurs.
}
});
This restriction is determined by the implementation mechanism of jsonp.
Solution:
Use a jquery plugin -- jquery-jsonp, https://github.com/jaubourg/jquery-jsonp
Sample Code:
The Code is as follows: