我正在尝试以角度$ http从api获取数据,但我收到了一个错误跨源请求被阻止:同源策略禁止在http:// **** / api读取远程资源/ / 的 /。这可以通过将资源移动到同一域或启用CORS来解决。下面是我的代码文件** ** app.js
var myApp = angular.module('abc',['ui.bootstrap','ngAnimate']);
controller.js
myApp.config(function($httpProvider) {
$httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
myApp.controller('careersCtrl', ['$scope','$http','dataService', function($scope,$http,dataService){
$scope.testVar = "hello";
dataService.dataSearch().then(function(dataResponse) {
$scope.data = dataResponse;
console.log('hello');
});
}]);
myApp.service('dataService', function($http) {
delete $http.defaults.headers.common['X-Requested-With'];
this.dataSearch = function() {
return $http({
method:'GET',
url:'http://*****.cc/api/****/****/',
headers:{
'Authorization': 'Token token=61d1802e-d539-4fe7-8110-1651053e6e0b'
}
});
}
});