$ http.get返回http代码401

时间:2013-10-15 11:51:45

标签: javascript ajax api angularjs

我正在尝试使用angularJS连接到我编写的API,目前API的根路径显示API的信息:

{
    "Company": "Test Company",
    "Version": "0.1"
}

当使用jquerys $.ajax()访问数据时,我按预期获得了json。但是当我在angularJS中使用$ http.get()时,控制台中返回的错误是:

OPTIONS http://testapi.dev/ 401 (Unauthorized) angular.min.js:100
OPTIONS http://testapi.dev/ Invalid HTTP status code 401 angular.min.js:100
XMLHttpRequest cannot load http://testapi.dev/. Invalid HTTP status code 401 

以下是我尝试提取数据的索引控制器的代码:

var index = angular.module('index', ['$strap.directives']);

index.factory('infoFactory', function ($http) {

    var info_url = 'http://testapi.dev/';
    var login_url = info_url + 'login';

    var info = {};

    return {
        info: info,

        getInfo: function () {
            return $http.get(info_url);
        }
    };
});

index.controller('indexCtrl', function indexCtrl ($scope, infoFactory) {

    infoFactory.getInfo().success(function (data) {
        if (data.Success) {
            $scope.info = data.Data;
            infoFactory.info = data.Data;
        } else {
            alert(data.Message);
        }
    });     
});

1 个答案:

答案 0 :(得分:0)

尝试此更改

   getInfo: function ($http) {
        return $http.get(info_url);
    }

将$ http注入你的getInfo函数