【发布时间】:2017-01-30 18:27:22
【问题描述】:
我有一个视图调用下面的这个函数,它对我们的 API 进行 AJAX 调用 - 由于某种原因,当我使用 Firefox DOM 检查工具在 AngularScope 中查看它时,它总是返回“未定义”。
如果我检查“网络”选项卡,我可以看到该 URL 已被调用并且可以看到我期望的 JSON,然后我想返回 data.words JSON 数据,但这总是返回未定义?如果我删除 AJAX 调用并在最后一次返回中留下“静态”和“单词”,则按预期工作,所以我很清楚 AJAX 成功调用中的返回似乎不正确...有什么想法吗??
// 在 AngularJS 服务文件中
this.getAccSignoffWords = function() {
var url = ApiService.getDomain() + 'account/signoff';
$http({
method : 'GET',
url : url
}).success(function(data) {
if (data.success) {
return data.words; // I want to return this JSON to the scope
}
}).error(function(data) {
throw "Failed to get sign-off words";
})['finally'](function(data) {
});
// return [ 'static', 'words' ]; // this line is commented out and only used for testing and is an example of the type of data expected from data.words
}
【问题讨论】:
-
尝试解析数据成功回调:
data= JSON.parse(data) -
嗨 Nishanth,这个应该放在哪里 - 我应该删除成功块中的当前返回
-
hmm 在成功块中使用它仍然没有乐趣.. :(
-
你的 ajax 没有返回任何东西..这只是一个回调......
标签: javascript angularjs json ajax angularjs-service