2013-10-15 49 views
0

我试图用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); 
     } 
    });  
}); 
+0

我觉得你的服务器应该实现CORS https://开头下发展r.mozilla.org/en-US/docs/Server-Side_Access_Control看看脚本扔Chrome deleloper工具它可能是非常有用的:) – Whisher

回答

0

尝试这种变化

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

进样$ HTTP到您的getInfo功能

+0

这只是导致它引发错误'TypeError:无法调用方法'获取'未定义的 – ThomasY

相关问题