This method is an advanced encapsulation of the preceding example $. ajax.
On the server side, obtain the callback parameter (for example, webpage special effect onp *****) to obtain
Then return the following code: "webpage special effect onp ***** (" + json array to be returned + ")";
Jquery will dynamically load and call this through the callback method: jsonp ***** (json array );
This achieves the purpose of cross-Origin data exchange.
Jsonp is a script injection action, so it also has certain security risks.
$. Ajax ({
Async: false,
Url: '', // cross-origin url
Type: 'get ',
Datatype: 'jsonp ',
Jsonp: 'jsoncallback', // default callback
Data: mydata, // request data
Timeout: 5000,
Beforesend: function () {// jsonp mode this method is not triggered. The reason may be that if datatype is specified as jsonp, it is no longer an ajax event.
},
Success: function (json) {// call back function pre-defined by jquery on the client. After obtaining json data on the Cross-origin server, the callback function is dynamically executed.
If (json. actionerrors. length! = 0 ){
Alert (json. actionerrors );
}
},
Complete: function (xmlhttprequest, textstatus ){
},
Error: function (xhr ){
// Jsonp mode. This method is not triggered.
// Handle request errors
Alert ("request error (Check related network conditions .)");
}
});
Jquery code
$. Getjson (url + "? Callback =? ",
Function (json ){
});