【问题标题】:How can i change cross origin blocked in angular我如何更改以角度阻塞的交叉原点
【发布时间】:2015-05-18 13:02:30
【问题描述】:

我正在尝试以角度 $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'
            }
         });
     }
});

【问题讨论】:

  • 您使用的是什么后端?这是必须在您的服务器上解决的问题

标签: angularjs http cors


【解决方案1】:

它没有被角度阻塞,你需要的是启用CORS 此处描述了最佳方法:

http://enable-cors.org

【讨论】:

    猜你喜欢
    • 2015-04-30
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2016-02-29
    • 2023-01-27
    • 2015-11-27
    • 1970-01-01
    相关资源
    最近更新 更多