【问题标题】:No 'Access-Control-Allow-Origin' ---> Doing an angular $HTTP request [duplicate]没有'Access-Control-Allow-Origin'--->做一个角度的$HTTP请求[重复]
【发布时间】:2015-01-20 21:42:00
【问题描述】:

我正在执行一个 ajax 获取请求,但出现此错误:

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)
        }
    })
    }

});

【问题讨论】:

  • domains.bootname.com 必须发送必要的 HTTP 标头以允许这样做。

标签: angularjs http cors angular-http


【解决方案1】:

我将请求更改为 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);
        });
    }

});

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 2021-03-05
    • 2018-12-16
    • 1970-01-01
    • 2018-03-28
    • 2015-11-09
    • 2019-02-28
    • 2017-11-14
    • 2021-09-25
    相关资源
    最近更新 更多