Javascript 在$http服务中,我如何捕获;“地位”;错误?

Javascript 在$http服务中,我如何捕获;“地位”;错误?,javascript,angularjs,Javascript,Angularjs,我正在读一本书,名叫《专业英语》。 但是,我有一个关于如何捕获错误状态的问题 我编码的是: $http.get(dataUrl) .success(function (data){ $scope.data.products = data; }) .error(function (error){ $scope.data.error=error; console.log($scope.data.error.status); //

我正在读一本书,名叫《专业英语》。 但是,我有一个关于如何捕获错误状态的问题

我编码的是:

$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的参数没有定义

书中有这样一句话:“传递给error函数的对象定义了状态和消息属性。”

所以我做了$scope.data.error.status


为什么出错?

响应状态作为回调()中的第二个参数出现:

更新:
从angularjs 1.5开始,promise方法success和error已经被弃用。(见附件)

发件人:


您可以使用函数的其他参数,如下所示:

error(function(data, status, headers, config) {
    console.log(data);
    console.log(status);
}
请参见文档:

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


正如您所看到的,错误回调的第一个参数是数据,状态是第二个。

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

摘自:

  • 数据{string | Object}–使用转换函数转换的响应体
  • 状态–{number}–响应的HTTP状态代码
  • headers–{function([headerName])}–Header getter函数
  • 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); 
  }); 

显然,您不必创建表示错误的对象,只需创建单独的作用域属性,但同样的原则也适用。

因为
$http.get
使用额外方便的方法
success
error
返回“promise”(只包装
然后
的结果)您应该能够使用(无论角度如何):


严格来说,这不是问题的答案,但如果你被“我的Angular版本不同于docs”问题困扰,即使你不知道合适的方法签名,你也可以随时转储所有
参数:

$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
  });

然后,根据您发现的任何内容,您可以“修复”函数参数以匹配。

已弃用了
$http
旧承诺方法
success
error
。改用标准的
然后
方法。看看文档$http

现在正确的使用方法是:

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});
响应对象具有以下属性:

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

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

如果只执行
console.log($scope.data.error),消息是什么?@Clawish则打印“未找到”。不过,我想打印的是“404”谢谢大家!!虽然这是我在这个网站上的第一个问题,但是有这么多有用的答案,我很高兴!这是一个非常好的网站!!成功与错误已被弃用:非常感谢!那么,这本书错了吗?在书中,“传递给错误函数的对象定义了状态和消息属性。”角度是否改变了?我记得是一样的。。确保您始终阅读文档,并且这些文档适用于您的angular版本。(这里有一个包含所有可用版本的下拉列表)非常感谢!那么,这本书错了吗?在书中,“传递给错误函数的对象定义了状态和消息属性。”角度是否已更改?谢谢Doctomick^^我在更改日志中看不到任何表明此更改的内容。从Angular 1.4开始,文档中现在有一个弃用通知,说明“已弃用$http legacy promise方法success and error。请改用标准then方法。”只是重申一下@Darryl,
。success
。error
现在已弃用。使用
.then(函数successCallback(response){},函数errorCallback(response){})取而代之。人们从Angular 1.4、
中读到这篇文章,成功的
和错误的
已经被弃用,取而代之的是
。然后
和Angular 1.6,
。成功的
。错误的
不再可用,你必须使用
。然后
和可能的
。catch
$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); 
  }); 
$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
    })
$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
  });
// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});