无极Angular.js不返回预期的文件 [英] Promise not returning the intended file in Angular.js
问题描述
function getJSON(url){
return $http.get(url).success(function(response){
var result = response['entries'][0];
return result;
});
};
我以为$ HTTP返回的承诺,所以我做了以下内容:
I thought that $http returned promises, so I did the following:
getJSON(url).then(function (result) {
console.log(result);
})
然而,当我登录的结果,我不明白我登录超时功能里面的JSON文件,而是我得到一个HTML的响应文件。这是我结束了:
However, when I log the result, I don't get the JSON file I logged inside the timeout function, but instead I get a HTML response file. This is what I ended up with:
Object {data: Object, status: 200, config: Object, statusText: "OK"}
你知道为什么它做了什么,我能做得到我在$超时功能,而不是记录的JSON文件?
Do you know why it does that and what I can do to get the JSON file I logged in the $timeout function instead?
推荐答案
你的方法改成这样:
function getJSON(url){
return $http.get(url).then(function(response){
var result = response.data['entries'][0];
return result;
});
};
PS:最初你在问题中有一个 $超时
,事情是不同的。在这种情况下 .success
应实际工作。唯一的问题是,你不能继续链接在 .success
电话后承诺
PS: initially you had a $timeout
in the question and things were different. In this case .success
should actually work. The only problem is that you can't continue chaining promises after the .success
call
这篇关于无极Angular.js不返回预期的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!