“没有& # 39;Access-Control-Allow-Origin& # 39;标头存在于请求的资源"

"No 'Access-Control-Allow-Origin' header is present on the requested resource" error for response from http://www.google.com/

本文关键字:请求 资源 quot Access-Control-Allow-Origin 没有 存在 于请求      更新时间:2023-09-26
//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'。

Access-Control-Allow-Origin 设置在来自服务器的响应上,而不是客户端请求上,以允许来自不同来源的客户端访问响应。

在您的示例中,http://www.google.com/不允许您的源访问响应。因此你不能读它。

有关CORS的更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

相关文章: