$.getjson () Cross-domain request instance detailed

Source: Internet
Author: User

A common Getjson in jquery to invoke and get a remote JSON string, convert it to a JSON object, and execute the callback function if successful. The prototype is as follows:

Jquery.getjson (URL, [data], [callback]) loads JSON data across domains.

URL: Send the requested address
data: (optional) Key/value parameters to be sent
callback: (optional) callback function on successful load

Primarily used for client-side capture of 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);
})
});

Server return 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);
})
});

Server return string:

The code is as follows Copy Code

jquery1706543070425920333_1324445763158 ({"Name": "Loogn", "Age": 23})

The returned string is a function called "jquery1706543070425920333_1324445763158", and the argument is

The code is as follows Copy Code
{' Name ': ' Loogn ', ' age ': 23}.

In fact, this very long function name is the role of callback= in the request path, I think it should be: The $.getjson method generates a reference to the callback method name, replace? The request above will become

http://localhost:2589/a.ashx?callback=jQuery1706543070425920333_1324445763158&_= 1324445763194, the server returns JSON to be processed, such as:

The code is as follows Copy Code

String cb = Context. Request["Callback"];
Context. Response.Write (CB + "(" + JSON +) ");

Parameter name callback can also be replaced by Jsoncallback, I think is afraid of conflict, jsoncallback should be priority detection, no further detection callback (no test!!) )

? is also a specific function name, so that the callback function can not be anonymous, with? The build is just a convenience that jquery provides for our general operations.

If it doesn't make sense, we'll look at one more example.


A.com uses $.getjson to send a request to the B.Com, and at the same time as the request, A.com puts the required delivery into the temporary file (or Membercache),

B.Com receives the request, in turn requests the required data to the A.com, carries out the necessary operation, then gives A.com returns the successful information. This achieves the goal of a cross-domain large volume of data request.

Of course, the communication process, preferably with the agreement, security.


JS Code:

The code is as follows Copy Code

$.getjson (target_url+ "jsoncallback=?"),
{
' userid ': <?php echo $userid;? >
},
function (Result) {
If (Result!= ' SC ') {
alert (result);
}else{
Top.document.getElementById ("menu"). src = $ (' #url_ ' +bid). Val () + "admin/left_menu.php";
Top.document.getElementById ("main"). src = $ (' #url_ ' +bid). Val () + "admin/index_body.php";
}
});

PHP Server-side code:

The code is as follows Copy Code

Header (' Content-encoding:plain ');
$userid = SS ($_request[' userid '));
Logical code
$msg = SC;
$json _str = Json_encode (Array ($msg));
echo $_request[' Jsoncallback ']. ' (' $json _str. ') ';
Exit

Header (' Content-encoding:plain ');

This sentence is very important, if there is no such sentence, under IE6 will not be able to return information, resulting in the callback function can not be executed.

Also found on the internet, not verified

The code is as follows Copy Code
<a href= "javascript:void (0);" ></a>

As a submit button will also cause IE6 under $.getjson error.

The submit button cannot be placed inside the <form> tag, otherwise the form interrupts the HTTP request issued by $.getjson.

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.