“在所请求的资源上不存在'Access-Control-Allow-Origin'标头"来自http://www.google.com/的响应错误 [英] "No 'Access-Control-Allow-Origin' header is present on the requested resource" error for response from http://www.google.com/
问题描述
//Create an Angular Module.
var newsModule = angular.module('NewsModule', []);
//Create an Angular Controller.
newsModule.controller('newsCtrl', ['$scope', '$http', function ($scope, $http) {
//function retrives POST,UPDATE,DELETE,GET data
$http.defaults.headers.put = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'
};
$http.defaults.useXDomain = true;
$scope.throughdata = function (){
delete $http.defaults.headers.common['X-Requested-With'];
$http.get('http://www.google.com').then(function(data,error){
alert(data);
alert(error);
$scope.days=data.data;
});
}
}
]);
但是我遇到以下错误
XMLHttpRequest无法加载 http://www.google.com/.所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问原始空".
XMLHttpRequest cannot load http://www.google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
推荐答案
Access-Control-Allow-Origin
是在服务器的响应上设置的,不是在客户端请求时设置的,以允许客户端不同的来源可以访问响应.
Access-Control-Allow-Origin
is set on the response from server, not on client request to allow clients from different origins to have access to the response.
对于您来说, http://www.google.com/不允许您的来源访问响应.因此,您无法阅读它.
In your case, http://www.google.com/ does not allow your origin to have access to the response. Therefore you cannot read it.
有关CORS的更多信息: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS
For more information about CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
这篇关于“在所请求的资源上不存在'Access-Control-Allow-Origin'标头"来自http://www.google.com/的响应错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!