Jquery ajax cross-origin request details

Source: Internet
Author: User

This article provides details about ajax cross-origin requests in jquery. JQuery provides two solutions for Ajax cross-origin requests, but they only support the get method. They are JQuery. ajax jsonp and jquery. getScript.

To achieve perfect cross-origin in various browsers, ajax requires the use of jsonp technology. jsonp actually requests a js script file and executes a function when the js file is loaded, in this way, the rule cross-origin problem can be perfectly solved.

What is jsonp format? Original API: If the obtained data file is stored on a remote server (the domain name is different, that is, cross-Origin data retrieval), The jsonp type is required. If this type is used, a query string parameter callback =? will be created? This parameter is added after the request URL. The server should add the callback function name before the JSON data to complete a valid JSONP request. This means that the remote server needs to process the returned data and return a callback (json) data according to the callback parameter submitted by the client, the client will process the returned data in script Mode to process json data. JQuery. getJSON also supports jsonp data calling.


Jquery encapsulates the jsonp request sending method, so that the jsonp request and ajax request method are almost the same. The following is the method of jquery cross-origin request:

The 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 that the difference between cross-origin requests and ajax requests is that dataType is set to jsonp.

The corresponding server 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 about php. Next let's take a look at


Front-end 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 ){
// Handle errors
}
});

Backend 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 added the js function call $ _ GET ['callback'] During the output. It is clear that a js function name is passed through the get parameter callback of the PHP file, jquery automatically sets this callback.

In addition, jquery also provides the $. getJSON () method, which encapsulates the above cross-origin request. When using this method, you must set the callback parameter of the request url ?.

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.