jquery Ajax cross-domain request detailed

Source: Internet
Author: User
Tags json

Ajax in a variety of browsers to achieve a perfect cross-domain needs with the help of JSONP technology, JSONP Essence is to request a JS script file, in the JS file load completed when a function, so you can perfect the rule cross-domain problem.

What is the JSONP format? Original API: If you get a data file that resides on a remote server (the domain name is different, that is, cross domain to get data), you need to use the JSONP type. Using this type, a query string parameter is created callback=? , this parameter is appended to the URL of the request. The server side should precede the JSON data with the callback function name in order to complete a valid JSONP request. This means that the remote server needs to process the returned data and return a callback (JSON) data based on the callback parameters submitted by the client, and the client will process the returned data in a script to handle the JSON data. The Jquery.getjson also supports the JSONP data method invocation.


jquery encapsulates the way the JSONP request is sent, making the JSONP request and AJAX requests almost identical, as follows: The jquery Cross-domain request method

  code is as follows copy code

$.ajax ({
     URL: "testserver.php",
     dataType: ' Jsonp ',//Note: Jsonp <--P ( lowercase)
     success:function (JSON) {
         //Do stuff with JSON (in this case an array)
         alert ("Success") ;
    },
     error:function () {
          alert ("Error");
    },
};

Note the difference between general Ajax requests for Cross-domain requests is that the datatype settings are JSONP.

The corresponding server-side code example is as follows:

The code is as follows Copy Code

<?php
$arr = Array ("Element1", "Element2", Array ("Element31", "Element32"));
$arr [' name '] = "response";
Echo $_get[' callback ']. "  (". Json_encode ($arr)."); "; 09/01/12 corrected the statement
?>

The above is the PHP, the following I look at A and asp.net


Foreground request Code

The code is as follows Copy Code

$.ajax ({
Type: "Get",
URL: "Http://www.xxx.com/Rest/ValidAccountsExists.aspx?accounts=admin",
DataType: "Jsonp",
JSONP: "Jsoncallback",
Success:function (Result) {
Alert (result. Success);
Alert (result. Content);
},

Error:function (result, status) {
Handling Errors
}
});

Background processing Code validaccountsexists.aspx

The code is as follows Copy Code
String accounts = gamerequest.getquerystring ("accounts");
String jsoncallback = gamerequest.getquerystring ("Jsoncallback");
Response.ContentEncoding =system.text.encoding.utf8;
Response.ContentType = "Application/json";
Response.Write (Jsoncallback + "({" Success ":" True "," Content ":" "+ Accounts +" "})");
Response.End ();


The above server code in the output added JS function call $_get[' callback ', it is obviously through the PHP file get parameters callback passed a JS function name, jquery will automatically set this callback.

Additionally, jquery also provides a $.getjson () method that encapsulates the cross domain request above, and that you need to set the callback parameter of the requested URL to? When using this method.

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.