jquery cross-Domain access solution

Source: Internet
Author: User
Tags domain server

Client "cross-domain Access" has been a headache problem, fortunately, jquery help, from the jQuery-1.2 after the cross-domain problem will be solved. Because of the cross-domain problem in the project, I take this opportunity to find out the cross-domain problem, consult the relevant data and own practice, which solves the cross-domain problem. are recorded for inspection.
Jquery.ajax () supports the cross-domain of Get mode, which is actually done in Jsonp way.
Real Case:
$.ajax ({
Async:false,
URL: ' http://www.mysite.com/demo.do ',//cross-domain URL
Type: ' GET ',
DataType: ' Jsonp ',
JSONP: ' Jsoncallback ',//default callback
Data:mydata,
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);
}
Gendynamiccontent (Qsdata,type,json);
},
Complete:function (XMLHttpRequest, Textstatus) {
$.unblockui ({fadeout:10});
},
Error:function (XHR) {
Jsonp mode This method is not triggered
Request Error Handling
Alert ("Request error (Please check correlation network status.)");
}
});

Attention:
$.getjson ("http://www.mysite.com/demo.do?name1=" +value1+ "&callback=?",
function (JSON) {
if (JSON. property name = = value) {
Execute code
}
});
This is actually a high-level package of the previous example $.ajax ({..}) API, and some of the $.ajax API's underlying parameters are encapsulated and not visible.

At the server end of the Request.getparameter ("callback"), the jquery end will be callback= back jsonp32440980
It then returns a similar: "jsonp32440980 (" + JSON array to return + ")";
jquery will invoke this by using the callback method to dynamically load: jsonp32440980 (JSON array);
This achieves the purpose of cross-domain data exchange.
Jsonp The basic principle is: Dynamic add one is consistent (QQ space is a large number of this way to achieve cross-domain data exchange). JSONP is a script injection (scripts injection) behavior, so there are some security implications.
Note: Jquey is not supported for post-mode cross-domain.
This is because although the use of post + dynamic generation of the IFRAME is possible to achieve the purpose of post cross-domain (there is a bit of JS Bull is this way to jquery1.2.5 patch), but this is a more extreme way, not recommended. It can also be said that the cross-domain get method is legal, post method from the security point of view is not legal, the last resort or not post,client end cross-domain access requirements seem to have attracted the attention of the world, see the data said HTML5 WebSocket standard support cross-domain data exchange, should also be a future-selectable solution for cross-domain data exchange.
Reference: http://www.jb51.net/article/21213.htm

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.