On AJAX requests and cross-domain requests in jquery

Source: Internet
Author: User

The code for the AJAX request data:

$ (function () {
$.ajax ({
url:url,//the URL where the request file resides
Type: "Get",//Request the way
Success:function (data) {
Console.log (data);//Return
}
})
})
The requested data is generally available, but sometimes the so-called cross-domain request problem occurs when requesting data from this domain name to another domain name.

Browsers are enabled by default on the same-origin policy, which refers to the AJAX request page and the requested page in the protocol, domain name, port are the same to be accessed , otherwise the following error will be prompted:

XMLHttpRequest cannot load xxxxxxx is not allowed by Access-control-allow-origin.
So how to solve this problem, jquery provides cross-domain request resolution;
The code is:
$(function() {
$. Ajax ({
URL:URL,//URL where the requested file resides
type:"Get",//method of request,
DataType:"Jsonp",//format of data
Jsonp:"Callback",
Success:function(data) {
Console.Log(data);//Data returned
}
})
})
or by:
Jquery.getjson ("url",(Result) {
console. Log (result);//Return results
})
If a script is requested, the value of the DataType property is changed to "script" when using the method above, or when the following method is used;
Jquery.getscript ("url",(Result) {
console. Log (result);//Return results
});

jsonp:"Callback",

On AJAX requests and cross-domain requests in jquery

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.