在 AngularJS 1.2rc(x) 中使用 ngResource
,现在如何获取状态代码?
RestAPI.save({resource}, {data}, function( response, responseHeaders ) {
});
其中 RestAPI
是我的 ngResource
。
响应包含 $promise
对象和从服务器返回的资源,但不再是状态。如果服务器将状态代码注入(inject)到 header 对象中,则 responseHeaders()
函数仅具有状态,而不是真正返回的状态代码。所以有些服务器可能会提供服务,有些可能不会。
最佳答案
您可以使用 Promiss 回调 then
, catch
和finally
$resource
之后打电话。
例如。如果你想在调用后捕获错误,你可以这样做:
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
对象将有 status
和 statusText
特性。 status
是一个整数状态代码和 statusText
文本。您还将拥有data
包含服务器响应的属性。
编辑:按照建议,它是 response.status
关于angularjs - 如何在 AngularJS 1.2 中获取 HTTP 响应状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729556/