gpt4 book ai didi

angularjs - 错误状态代码未被捕获

转载 作者:行者123 更新时间:2023-12-01 00:37:54 26 4
gpt4 key购买 nike

我正在 try catch angular 资源的 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 代码:
.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(function(result)(...) 中的代码已执行,但我无法检测到响应 HTTP 状态代码。

所以,我的问题:
  • 为什么我的错误处理函数没有被执行?
  • 如何检测 HTTP 响应状态代码?
  • 最佳答案

    第一.catch转换 拒绝履行。为防止转换,.catch方法需要throw错误。

       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 诊断问题方法,添加响应拦截器:
    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',

    要寻找的另一件事是其他 $http interceptors在应用程序中 转换 省略 的响应扔陈述。

    关于angularjs - 错误状态代码未被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39166536/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com