在角$ http服务,我如何能赶上"状态"错误? [英] In angular $http service, How can I catch the "status" of error?

查看:207
本文介绍了在角$ http服务,我如何能赶上"状态"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一本书叫临角JS。
不过,我有一个关于如何捕捉错误的状态问题。

I'm reading a book called, "Pro Angular JS". However, I have a question about how to catch a status of error.

I codeD是什么:

What I coded is :

$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.)

    });

如果我code的console.log($ scope.data.error.status); ,为什么执行console.log的说法是不确定的?

If I code "console.log($scope.data.error.status);" , why does the argument of console.log is undefined?

在这本书中,有一句话,传递给误差函数的对象定义的状态和消息属性。

In the book, there are sentence, "The object passed to the error function defines status and message properties."

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

So I did $scope.data.error.status

为什么错了?

推荐答案

您的论点是不正确的,错误不会返回包含状态和消息的对象,它通过他们在下面说明的顺序不同的参数。

Your arguments are incorrect, error doesn't return an object containing status and message, it passed them as separate parameters in the order described below.

角文档措施:


  • 数据 - {字符串|对象} - 与变换函数变换的响应体。

  • 状态 - {数} - 响应的HTTP状态code

  • 标题 - {功能([headerName])} - 头getter函数

  • 配置 - {对象} - 这是用于生成请求的配置对象。

  • 状态文本 - {string}里 - 响应的HTTP状态文字

所以,你需要改变你的code为:

So you'd need to change your code to:

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

当然,你不必创建一个对象重新presenting错误,你可以只创建单独的范围,性质,但同样的原则也适用。

Obviously, you don't have to create an object representing the error, you could just create separate scope properties but the same principle applies.

这篇关于在角$ http服务,我如何能赶上"状态"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆