Jquery $. getJSON () Cross-origin request

Source: Internet
Author: User

1. The same domain name and other requests can be the same
Js:
Copy codeThe Code is as follows:
Var url = "http: // localhost: 2589/a. ashx ";
$ (Function (){
$. GetJSON (url, function (data ){
Alert (data. Name );
})
});

The server returns a string:
{"Name": "loogn", "Age": 23}
2. Under Different domain names
Js:
Copy codeThe Code is as follows:
Var url = "http: // localhost: 2589/a. ashx? Callback =? ";
$ (Function (){
$. GetJSON (url, function (data ){
Alert (data. Name );
})
});

The server returns a string:
Jquery1706543070116920333_1324445763158 ({"Name": "loogn", "Age": 23 })
The returned string is a function called "jQuery1706543070425920333_1324445763158". The parameter is {"Name": "loogn", "Age": 23 }.
In fact, this long function name is callback = in the Request Path? I think it should be like this: $. getJSON method generates a reference name for the callback method, replace ?. The above request will become
Http: // localhost: 2589/a. ashx? Callback = jQuery1706543070425920333_1324445763158 & _ = 1324445763194. You need to handle this when the server returns the json file, for example:
Copy codeThe Code is as follows:
String cb = context. Request ["callback"];
Context. Response. Write (cb + "(" + json + ")");

The parameter name callback can also be changed to jsoncallback. I think we are afraid of conflict. jsoncallback should be checked first and no callback detection will be performed (not tested !!)
? But also the specific function name, so that the callback function cannot be anonymous, use? Generation is just a convenience provided by jQuery for our general operations.

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.