3

我的 AngularJS 项目中的 $http 无法识别 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 中运行得非常好。请帮我解决这个问题。

提前致谢 :)

4

3 回答 3

5

所以你 r 使用$httpfrom 角度。你在回调中使用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参数还包含错误代码。

考虑使用 a$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记录了一些东西。

于 2014-05-06T06:08:31.057 回答
2

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

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

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

我希望这有帮助。

于 2016-02-08T07:25:13.937 回答
0

我对 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);
                }
            };
        });
于 2020-05-14T20:33:30.487 回答