Jquery Ajax cross-origin solution (in JSON format)

Source: Internet
Author: User

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.

Related Article

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.