Recently, many projects developed by the company require cross-origin Ajax requests, such as several subdomains.
Http: // A. *****. com/index123.aspx,
Http: // B. *****. com/index2.aspx
Request the JSON information of the user, and then process the data. At first, my colleagues and I tried many ways to use $. ajax (), whether it is get or post, will cause URI deny errors. Some Gg later found the solution and understood the reasons.
Since jquery 1.2,. getjson supports cross-origin operations. The jquery. getjson () method can be used to solve cross-origin problems. The instance is as follows:
Front-end
<SCRIPT type = "text/JavaScript" src = "/script/jquery. js"> </SCRIPT>
JS Code in HTML
Function gettst2 (){
$. Getjson ("http://ucenter.xxxx.com.cn/ajax/test.aspx? Callback =? ",{ ID:" 123456 ", site:" 01 "},
Function (data ){
Alert(data.html S );
Document. getelementbyid ("shows"). innerhtml = data.html S;
});
}
Gettst2 ();
Processing in the Aspx. CS file is
String jsoncall = request. querystring ("Callback ");
Response. Write (jsoncall + "({htmls: Test 001 })");
If you addHTML codeDo not add the/n symbol. Otherwise, garbled characters and JS errors may occur.