No ' Access-control-allow-origin ' Ajax cross-domain access solution

Source: Internet
Author: User

No ' Access-control-allow-origin ' header is present on the requested resource.

When using AJAX to access a remote server, the request fails and the browser is reported as an error. This is due to security considerations, which prohibit cross-domain access by default.

One, what is cross-domain access

For a chestnut: In a site, we want to use Ajax to get specific content from the B site. Cross-domain access issues occur if the A site is not in the same domain as the B site. You can understand that two domain names cannot cross a domain name to send requests or request data, otherwise it is unsafe. Cross-domain access violates the same Origin policy, and the details of the same-origin policy can be accessed by clicking the following link: same-origin_policy;
In summary, the same-origin policy stipulates that the browser's Ajax can only access resources that are identical to its HTML page (the same domain name or IP).

Second, the solution

There are two common solutions, which can be divided into client solutions and server-side solutions. First, the server-side solution:

Server-Side Solutions
Add in the server-side filter or servlet
Response.setheader ("Access-control-allow-origin", "*");
"Access-control-allow-origin" means that cross-domain access is allowed, and "*" means that all sources are allowed cross-domain access, which can also be replaced with a specific domain name or IP.
Obviously, this approach is not possible for non-site owners. And this approach is vulnerable to csrf attacks.

Client Solutions

$ (function ($) {
var url = ' Http://*****/index ';
$.ajax (URL, {
Data: {
' CityName ': ' Chengdu ',
' Date ': ' 2016.12.12 '
},
DataType: ' Jsonp ',
Crossdomain:true,
Success:function (data) {
if (data && Data.resultcode = = ' 200 ') {
Console.log (Data.result.today);
}
}
});

Set the DataType property in the AJAX request to "Jsonp", JSONP is specifically designed to address cross-domain access.

No ' Access-control-allow-origin ' Ajax cross-domain access solution

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.