【问题标题】:$http error handling in AngularJSAngularJS 中的 $http 错误处理
【发布时间】:2014-06-22 14:32:42
【问题描述】:

$http 在我的 AngularJS 项目中无法识别 iOS 上的 40X(401,403,405...) 错误。 我使用的是 1.2.10 AngularJS 版本和 Cordova 版本 3.4.0。

下面是我正在使用的代码:

TE_SERVICES.factory('hello',function ($http,$rootScope) {
return {
loginUser: function(userCredentials,successCallback,errorCallback){
          $http({
               method: "GET",
               url: "data/example.json",
               headers: {"Authorization":'Basic '+userCredentials},
             }).then(function(response){
                                successCallback(response.data);
                                console.log("Success------"+JSON.stringify(response))
             },function(data, status, headers, config){
                                errorCallback(data);
                                console.log("Error------"+JSON.stringify(data)+" "+status)
             })

        }
    }
 });

hello.loginUser($rootScope.encodedUserCredencials,function(persons) {
                // success handler
            }, function(data) {
                // error handler
                console.log(data.status+"===="+status)
                });

data.status 返回 0,状态返回未定义。 请帮我解决这个问题。

试图将域包含在 IOS 的白名单中。但没有解决方案 :( 它仍然给出相同的响应。

但相同的代码在 Android 中运行良好。 请帮我解决这个问题。

提前致谢:)

【问题讨论】:

  • 我也有同样的问题。你找到解决办法了吗?

标签: ios angularjs cordova-3 angularjs-http


【解决方案1】:

所以你使用 Angular 中的 $http。您使用error 回调还是then 回调中的第二个函数?

例子

$http.get("someUrl")
  .success(function(response){}) // if http code == 200
  .error(function(response){}) // else

或者使用then,可以有2个功能。第一个是 onSuccess,第二个是 onError 函数。

$http.get("someUrl")
  .then(function(response){
            // if http code == 200
   },
    function(response){
            // else
   }); 

response 参数也包含错误代码。

考虑使用$httpInterceptor 在同一位置处理所有错误代码,而不是在每个 http 回调中处理它们。

更新: 看来,角度文档对于成功回调是不完整/错误的。 它没有在那里传递 4 个参数。它确实传递了一个 response 对象,其中包含有关请求+响应和传递的数据的所有信息。

更新编辑

不要自己写回调。使用角度承诺:

TE_SERVICES.factory('hello',function ($http,$rootScope) {
   return {
       loginUser: function(userCredentials){
          return $http({
             method: "GET",
             url: "data/example.json",
             headers: {"Authorization":'Basic '+userCredentials},
          }).then(function(response){
             return response.data;                                    
          },function(response){
             return response;                                
          });    
        }
    }
 });

hello.loginUser($rootScope.encodedUserCredencials)
     .then(function(persons) {                // success handler

     }, function(data) { // error handler
       console.log(data);
     });

试试这个,告诉我console.log 是否记录了什么。

【讨论】:

  • 我正在使用 then ,已编辑问题。请看一下。
  • 我在 android 中工作正常,问题出在 IOS 中,当我在 android 中提供无效凭据时,它会引发 401 错误,但在 IOS 中它显示 data.status 为 0.:(
  • 根据docs.angularjs.org/api/ng/service/$http "$http 旧承诺方法successerror 已被弃用。改用标准then 方法。"
【解决方案2】:

我遇到了完全相同的问题。 angular js http 拦截器中未收到 Cordova 应用程序、angular js、IPhone 和 401 请求。它们在安卓设备上运行良好。

我的发现是,iPhone 浏览器正在以更高的水平处理这些问题,并尝试使用 WWW-Authenticate 信息进行身份验证。这就是响应没有达到角度的原因。

我找到的唯一解决方案是将我的服务更改为在 api 请求的情况下返回 400 而不是 401。在这种情况下,我返回 400 并带有一条我在客户端处理的错误消息。

我希望这会有所帮助。

【讨论】:

    【解决方案3】:

    我对 403 状态代码的问题是我的后端返回了状态为 403 的响应,但响应的正文不包含 JSON 字符串。它只包含一个字符串 - 身份验证失败。

    拒绝变量是一个对象错误。 我对正文进行了编码,并且拒绝变量包含一个有效的响应错误对象。 为了处理 HTTP 错误,我使用了拦截器。

    $httpProvider.interceptors.push(function($q, $location, redirect, HTTP_CODES) {
                return {
                    'responseError': function(rejection) {
                        if (rejection.status === HTTP_CODES.FORBIDDEN) {
                            redirect('/login', $location.url());
                        }
    
                        return $q.reject(rejection);
                    }
                };
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 2016-09-29
      • 2016-08-06
      相关资源
      最近更新 更多