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