Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 未捕获错误状态代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图捕获角度资源的http错误状态代码(!= 200).
我的服务,我有资源定义:
(apiservice.js)

.factory('Apiservice',function($resource,$http,localStorageservice,CONfig) {

    var base_api_url = api_url = CONfig.api_url,api_version_prefix = CONfig.api_version_prefix;

    return {
        userDevices: $resource(api_url+'/requestRegistration/userDevices/:action',{},{
            registerDevice: {
                method: 'POST',params: {
                   action: ''
                }
            },verify: {
                method: 'POST',params: {
                   action: 'verify'
                }
            },}
    }
});

我的控制器代码

.controller('LoginCtrl',function(CONfig,$scope,$state,$ionicPlatform,$ionicPopup,ApiservicE) {


    $scope.data = {
        username: null
    };

    $scope.registerDevice = function() {
        if($scope.data.userName) { 
            var authenticationResponse = Apiservice.userDevices.registerDevice({
                username: $scope.data.username
            });

            authenticationResponse.$promise.then(function(result) {
                // this is always fired,even then response code is 400 or 503 :( I am not able to check response status code.
                console.log(result);
                console.log('success!');
            },function(error){
                // this code is not being exectued even when response status code is different then 200
                // its never executed at all :(
                console.log('error!');
            });
        }
    };


});

当我发送请求并收到响应代码400/503时,我认为应该执行功能(错误)代码,但事实并非如此.

相反,我的$promise.then中的代码(函数(结果)(…)被执行,我无法检测响应http状态代码.

所以,我的问题:

>为什么我的错误处理函数没有被执行?
>如何检测http响应状态代码

解决方法

一个.catch将拒绝转换为已完成.为防止转换,.catch方法需要抛出错误.

authenticationResponse.$promise.catch(function(error){
        alert('catched error!!!');
        //throw to chain error
        throw error;
    }).then(function(result) {
        // this is always fired,even then response code is 400 or 503 :(
        console.log(result);
        console.log('success!');
        //return to chain data
        return result
    },function(error){
        // This should be executed when status code is different then 200?
        // its never executed at all :(
        console.log('error!');
        //throw to chain rejection
        throw error;
    });

函数省略return或throw语句时,它返回undefined. $q服务创建派生的promise,解析为undefined.

诊断ngresource问题

要诊断$resource方法的问题,请@L_696_24@响应拦截器:

userDevices: $resource(api_url+'/requestRegistration/userDevices/:action',{
        registerDevice: {
            method: 'POST',params: {
               action: ''
            },interceptor: {
                response: function (responsE) {
                    console.log("registerDevice success");
                    console.log(response.status);
                    return response;
                },errorResponse: function (errorResponsE) {
                    console.log("registerDevice error");
                    console.log(errorResponse.status);
                    throw errorResponse;
                }
            }
        },verify: {
            method: 'POST',

要寻找的另一件事是App中的其他$http interceptors通过省略throw语句来转换响应.

大佬总结

以上是大佬教程为你收集整理的angularjs – 未捕获错误状态代码全部内容,希望文章能够帮你解决angularjs – 未捕获错误状态代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。