我正在读一本名为" Pro Angular JS"的书。 但是,我有一个关于如何捕获错误状态的问题。
我编码的是:
$http.get(dataUrl)
.success(function (data){
$scope.data.products = data;
})
.error(function (error){
$scope.data.error=error;
console.log($scope.data.error.status); // Undefined!
// (This is the spot that I don't get it.)
});
如果我编码" console.log($ scope.data.error.status);" ,为什么console.log的参数未定义?
在书中,有句子,"传递给错误函数的对象定义了状态和消息属性。"
所以我做了$ scope.data.error.status
为什么这是错的?
答案 0 :(得分:71)
已弃用Category
遗留承诺方法$http
和success
。请改用标准error
方法。查看文档https://docs.angularjs.org/api/ng/service/ $ http
现在正确的使用方法是:
then
响应对象具有以下属性:
200到299之间的响应状态代码被视为成功状态,将导致调用成功回调。
答案 1 :(得分:48)
您的参数不正确,错误没有返回包含状态和消息的对象,它按照下面描述的顺序将它们作为单独的参数传递。
取自angular docs:
因此,您需要将代码更改为:
$http.get(dataUrl)
.success(function (data){
$scope.data.products = data;
})
.error(function (error, status){
$scope.data.error = { message: error, status: status};
console.log($scope.data.error.status);
});
显然,您不必创建表示错误的对象,您可以创建单独的范围属性,但适用相同的原则。
答案 2 :(得分:10)
更新: 从angularjs 1.5开始,promise方法成功和错误已被弃用。 (见this answer)
来自current docs:
$http.get('/someUrl', config).then(successCallback, errorCallback);
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
您可以使用函数的其他参数,如:
error(function(data, status, headers, config) {
console.log(data);
console.log(status);
}
参见 $http 文档:
// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
答案 3 :(得分:7)
由于$http.get
使用额外的便捷方法success
和error
(只包装then
的结果)返回'promise',您应该能够使用(无论你的Angular版本如何):
$http.get('/someUrl')
.then(function success(response) {
console.log('succeeded', response); // supposed to have: data, status, headers, config, statusText
}, function error(response) {
console.log('failed', response); // supposed to have: data, status, headers, config, statusText
})
不严格地回答这个问题,但是如果你被“我的Angular版本与文档不同”问题所困扰,你可以随时转储所有arguments
,即使你不这样做t知道适当的方法签名:
$http.get('/someUrl')
.success(function(data, foo, bar) {
console.log(arguments); // includes data, status, etc including unlisted ones if present
})
.error(function(baz, foo, bar, idontknow) {
console.log(arguments); // includes data, status, etc including unlisted ones if present
});
然后,根据您找到的任何内容,您可以“修复”要匹配的函数参数。
答案 4 :(得分:6)
// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
正如您所看到的,错误回调的第一个参数是数据,状态是第二个。
答案 5 :(得分:3)
响应状态是回调中的第二个参数,(from docs):
// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});