如何在 AngularJS 1.2 中获取 HTTP 响应状态代码 [英] How do I get the HTTP response status code in AngularJS 1.2

查看:79
本文介绍了如何在 AngularJS 1.2 中获取 HTTP 响应状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 AngularJS 1.2rc(x) 中使用 ngResource,我现在如何获取状态码?

Using ngResource in AngularJS 1.2rc(x), how do I get the status code now?

RestAPI.save({resource}, {data}, function( response, responseHeaders ) {
});

其中 RestAPI 是我的 ngResource.

响应具有 $promise 对象和从服务器返回的资源,但不再是状态.responseHeaders() 函数仅在服务器将状态代码注入标头对象时才具有状态,而不是返回的真实状态代码.所以有些服务器可能会提供它,有些可能不会.

The response has the $promise object and the resource returned from the server but not a status anymore. The responseHeaders() function only has a status if the server injects the status code into the header object, but not the true returned status code. So some servers may serve it and some might not.

推荐答案

你可以使用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

这篇关于如何在 AngularJS 1.2 中获取 HTTP 响应状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆