Home > Web Front-end > JS Tutorial > Implementation method of using jsonp cross-domain access under jquery_jquery

Implementation method of using jsonp cross-domain access under jquery_jquery

WBOY
Release: 2016-05-16 18:22:08
Original
1144 people have browsed it
Copy code The code is as follows:

$.ajax({
async:false,
url: '', // Cross-domain URL
type: 'GET',
dataType: 'jsonp',
jsonp: 'jsoncallback', //Default callback
data: mydata, // Request data
timeout: 5000,
beforeSend: function(){ //jsonp 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) { //The callback function predefined by jquery on the client side. After successfully obtaining the json data on the cross-domain server, this callback function will be dynamically executed
if(json.actionErrors.length!= 0){
alert(json.actionErrors);
}

},
complete: function(XMLHttpRequest, textStatus){

},
error: function(xhr){
//Jsonp mode this method is not triggered
//Request error handling
alert("Request error (please check the correlation network status.)");
}
});



Copy code The code is as follows:

$.getJSON(url "?callback=?",
function(json){

});

This method is actually the above example A high-level wrapper for $.ajax({..}).

On the server side, get the callback parameter (such as: jsonp*****) to get the subsequent callback on the jQuery side
and then return something like: "jsonp*****(" the json to be returned Array ")";
jquery will dynamically load and call this through the callback method: jsonp*****(json array);
This achieves the purpose of cross-domain data exchange.

JSONP is a kind of script injection (Script Injection) behavior, so it also has certain security risks.

Note: jquey does not support cross-domain posting.
Reference:http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template