Implementation method of cross-domain access using JSONP under jquery

Source: Internet
Author: User
Tags domain server

$.ajax ({
Async:false,
URL: ',//cross-domain 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 datatype, if specified as Jsonp, is not an AJAX event.
},
Success:function (JSON) {//client jquery pre-defined callback function, after successfully acquiring JSON data on a cross-domain server, this callback function is executed dynamically
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 correlation network status.)");
}
});



Copy CodeThe code is as follows:
$.getjson (url+ "? callback=?",
function (JSON) {

});


This is actually an advanced package of the example $.ajax ({.}).

On the server side by obtaining the callback parameter (for example: jsonp*****) to get the jquery end to callback later
It then returns a similar: "jsonp***** (" + JSON array to return + ")";
jquery will invoke this by using the callback method to dynamically load: jsonp***** (JSON array);
This achieves the purpose of cross-domain data exchange.

JSONP is a script injection (scripts injection) behavior, so there are some security implications.

Implementation method of cross-domain access using JSONP under jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.