从http://www.google.com/的响应中出现了"No 'Access-Control-Allow-Origin' header is present on the requested resource"错误。

16 浏览
0 Comments

从http://www.google.com/的响应中出现了"No 'Access-Control-Allow-Origin' header is present on the requested resource"错误。

创建一个Angular模块。

var newsModule = angular.module('NewsModule', []);

创建一个Angular控制器。

newsModule.controller('newsCtrl', ['$scope', '$http', function ($scope, $http) {

函数获取POST,UPDATE,DELETE,GET数据

$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'。

0