【发布时间】:2015-05-21 20:16:25
【问题描述】:
我只是在使用 Angular 的 $http 模块测试超时功能,但是它一直返回 undefined 作为响应
这样设置也没问题,但如果我在$http 调用中添加.error(function) 而不是.then(function),它会在尝试从未定义对象中获取数据字段时抛出错误
var timeout = $q.defer();
var config = {
url: 'http://192.168.1.65:3000',
timeout: timeout.promise,
method: 'POST'
};
$http(config).then(function(res) {
// This is always undefined when timeout occurs
console.log(res);
});
$timeout(function() {
console.log('resolving the promise to abort the http call. This works fine');
timeout.resolve();
}, 1000);
任何想法我做错了什么?
【问题讨论】:
-
您没有将值传递给您的
resolve调用。 -
我不认为这是问题所在,我只是尝试进行健全性检查,并没有改变任何东西......根据这个其他答案,这应该就是你所需要的
-
您是要超时还是取消请求?我不得不在我的工作地点使用类似的东西,我不认为
timeout接受承诺。如果您要取消请求,请将cancellationToken设置为承诺。你还用了什么其他答案? -
@gfunk,当请求超时时,它是一个被拒绝的承诺 - 所以你需要
.catch -
哦,对不起...这是答案stackoverflow.com/a/21916315/3325262
标签: angularjs