$. GetJSON () is an asynchronous ajax transmission function. The following section describes how to use $. getJSON () in jquery to implement cross-origin requests. I hope this method will be helpful to you.
In jQuery, getJSON is often used to call and obtain a remote JSON string and convert it to a JSON object. If it is successful, the callback function is executed. The prototype is as follows:
JQuery. getJSON (url, [data], [callback]) loads JSON data across domains.
• Url: the address at which the request is sent
• Data: (optional) key/value parameter to be sent
• Callback: (optional) callback function for successful Loading
It is mainly used by clients to obtain server JSON data.
1. The same domain name and other requests can be the same
Js:
The Code is as follows: |
Copy code |
Var url = "http: // localhost: 2589/a. ashx "; $ (Function (){ $. GetJSON (url, function (data ){ Alert (data. Name ); }) }); |
The server returns a string:
The Code is as follows: |
Copy code |
{"Name": "loogn", "Age": 23} |
2. Under Different domain names
Js:
The Code is as follows: |
Copy code |
Var url = "http: // localhost: 2589/a. ashx? Callback =? "; $ (Function (){ $. GetJSON (url, function (data ){ Alert (data. Name ); }) }); |
The server returns a string:
The Code is as follows: |
Copy code |
Jquery1706543070116920333_1324445763158 ({"Name": "loogn", "Age": 23 }) |
The returned string is a function called "jQuery1706543070425920333_1324445763158". The parameter is
The Code is as follows: |
Copy code |
{"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:
The Code is as follows: |
Copy code |
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.
If you do not understand the above, let's look at another instance.
A.com uses $. getJson to send a request to B .com. At the same time as the request, a.com puts the required content in a temporary file (or membercache ),
B .com receives the request and, in turn, performs necessary operations on the data required by the request to a.com, and returns a successful message to a.com. In this way, the cross-domain large data volume request is achieved.
Of course, it is best to add a Protocol to the communication process to ensure security.
JS Code:
The Code is as follows: |
Copy code |
$. GetJSON (target_url + "? Jsoncallback =? ", { 'Userid': <? Php echo $ userid;?> }, Function (result ){ If (result! = 'Scs '){ Alert (result ); } Else { Top.doc ument. getElementById ("menu"). src =$ ('# url _' + bid). val () + "admin/left_menu.php "; Top.doc ument. getElementById ("main"). src =$ ('# url _' + bid). val () + "admin/index_body.php "; } }); |
PHP server code:
The Code is as follows: |
Copy code |
Header ('content-Encoding: plain '); $ Userid = SS ($ _ REQUEST ['userid']); // Logic code $ Msg = SC; $ Json_str = json_encode (array ($ msg )); Echo $ _ REQUEST ['jsoncallback']. '('. $ json_str .')'; Exit; Header ('content-Encoding: plain '); |
This sentence is very important. Without this sentence, information cannot be returned in IE6, And the callback function cannot be executed.
The statement found on the Internet has not been verified.
The Code is as follows: |
Copy code |
<A href = "javascript: void (0);"> </a> |
As the submit button, $. getJSON under ie6 may also fail.
The submit button cannot be placed in the <form> label. Otherwise, the http request sent by $. getJSON is interrupted by the form.