“请求的资源上不存在‘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"标头.因此,不允许访问原点 'null'.
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/en-US/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屋!