Implementation method of Cross-domain access using JSONP in jquery _jquery

Source: Internet
Author: User
Tags error handling domain server
Copy Code code 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 mode This method is not triggered. The reason may be that datatype, if specified as JSONP, is no longer an AJAX event.
},
Success:function (JSON) {//client jquery predefined callback function that dynamically performs this callback function after successfully acquiring JSON data on a cross-domain server
if (json.actionerrors.length!=0) {
alert (json.actionerrors);
}

},
Complete:function (XMLHttpRequest, Textstatus) {

},
Error:function (XHR) {
Jsonp way This method is not triggered
Request Error Handling
Alert (Please check the correlation network condition for errors);
}
});



Copy Code code as follows:

$.getjson (url+ "callback=?"),
function (JSON) {

});

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

In the server by obtaining the callback parameters (such as: jsonp*****) to get the jquery side then callback
It then returns a similar: "jsonp***** (" + JSON array to return + ");
jquery dynamically loads the call through the callback method: jsonp***** (JSON array);
This achieves the goal of cross domain data exchange.

JSONP is a scripting Injection (script injection) behavior, so there are some security risks.

Note: Jquey is not supported for post-domain cross-domain.
Reference: http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/

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.