【发布时间】:2017-05-16 03:59:53
【问题描述】:
我有一个 Angular 服务,我正在尝试设置以从节点服务器检索数据,该节点服务器向应用程序提供 JSON 文件。我已经检查了节点服务器 URL,它们都按照应有的方式输出 JSON,但是由于 HTTP 标头,Angular 似乎无法获取它们。从一些谷歌搜索来看,这似乎是一个常见问题,我只是不确定如何解决,甚至是否有解决方法?
eventsApp.factory('eventData', function($http, $log) {
return {
getEvent: function(successcb) {
$http({method: 'GET', url: 'http://localhost:8000/data/event/1'}).
success(function(data, status, headers, config){
successcb(data);
}).
catch(function(data, status, headers, config){
$log.warn(data, status, headers, config);
})
}
}
});
在控制台中,我收到以下警告:
Object { data: null, status: 0, headers: headersGetter/
【问题讨论】:
-
您应该使用
.then()而不是.success(),因为.success()已被弃用。.then()有一个response参数,您应该可以从中访问标头。 -
我对 Angular 完全陌生(这周才开始学习),所以对我来说很简单,但是除了用 then 替换成功之外,这将如何改变上面的代码?
-
使用控制台中错误的屏幕截图更新您的帖子,以提供更多详细信息
-
不要将回调与 promise API 一起使用。他们打破了承诺链并挂在错误条件上。见Why are Callbacks from Promise Then Methods an Anti-Pattern?。
-
状态 0 表示可能存在 CORS 问题。
标签: javascript angularjs json node.js