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.