没有'Access-Control-Allow-Origin'--->执行角度$ HTTP请求

时间:2014-11-22 22:54:43

标签: angularjs http cors angular-http

我正在做一个ajax get请求,我收到了这个错误:

XMLHttpRequest cannot load http://domains.bootname.com/api/v1/domain/test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. 

我不知道如何解决这个问题......这是我的JS:

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

myApp.controller('mainController', function($scope, $http){
    $scope.message = "hello";

    var myDomain = $scope.myDomain;

    $scope.searchDomain = function(myDomain){
        $http.get('http://domains.bootname.com/api/v1/domain/' + myDomain, {
        type: 'GET',
        dataType: 'JSONP',
        success: function(results){
            console.log(results)
        }
    })
    }

});

1 个答案:

答案 0 :(得分:2)

我将请求更改为jsonp并将“callback = JSON_CALLBACK”添加到URL请求的末尾。

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

myApp.controller('mainController', function($scope, $http){
    $scope.message = "hello";

    var myDomain = $scope.myDomain;



    $scope.searchDomain = function(myDomain){
        $http.jsonp('http://domains.bootname.com/api/v1/domain/' + myDomain + '?callback=JSON_CALLBACK').then(function(result){
            console.log(result.data);
        });
    }

});