在angular $ http服务中,我如何捕获错误的“状态”?

时间:2014-12-16 15:08:53

标签: javascript angularjs

我正在读一本名为" 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

为什么这是错的?

6 个答案:

答案 0 :(得分:71)

已弃用Category遗留承诺方法$httpsuccess。请改用标准error方法。查看文档https://docs.angularjs.org/api/ng/service/ $ http

现在正确的使用方法是:

then

响应对象具有以下属性:

  • data - {string | Object} - 使用转换函数转换的响应体。
  • status - {number} - 响应的HTTP状态代码。
  • headers - {function([headerName])} - 标头getter函数。
  • config - {Object} - 用于生成请求的配置对象。
  • statusText - {string} - 响应的HTTP状态文本。

200到299之间的响应状态代码被视为成功状态,将导致调用成功回调。

答案 1 :(得分:48)

您的参数不正确,错误没有返回包含状态和消息的对象,它按照下面描述的顺序将它们作为单独的参数传递。

取自angular docs

  • data - {string | Object} - 使用转换函数转换的响应体。
  • status - {number} - 响应的HTTP状态代码。
  • headers - {function([headerName])} - Header getter function。
  • config - {Object} - 用于生成请求的配置对象。
  • statusText - {string} - 响应的HTTP状态文本。

因此,您需要将代码更改为:

$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使用额外的便捷方法successerror(只包装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)

来自官方angular documentation

// 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.
  });