//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”来源。
ngResource
来完成这些类型的任务。它是用来覆盖$http
的,使得工作更加容易。 - yurisich