且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 AngularJS 1.2 中获取 HTTP 响应状态代码

更新时间:2022-05-04 05:18:26

你可以使用promiss回调thencatchfinally after$resource 调用.

You can use the promiss callbacks then, catch and finally after the $resource call.

例如.如果你想在调用后捕捉错误,你可以这样做:

For example. If you want to catch an error after a call, you would do something like this:

RestAPI.save({resource}, {data}, callbackFunction).$promise.catch(function(response) {
    //this will be fired upon error
    if(response.status == 500) alert('Something baaad happend');
}).then(function() {
    //this will be fired upon success
});

response 对象将具有 statusstatusText 属性.status 是一个整数状态代码,statusText 是文本.您还将拥有包含服务器响应的 data 属性.

The response object will have status and the statusText properties. status being an integer status code and statusText the text. You'll also have the data property containing the server response.

按照建议,它是 response.status