How do I get the HTTP response status code in AngularJS 1.2

前端 未结 8 1823
半阙折子戏
半阙折子戏 2020-12-05 03:04

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

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


        
8条回答
  •  感情败类
    2020-12-05 03:20

    You must add an interceptor inside your resource declaration. Like this:

    var resource = $resource(url, {}, {
        get: {
            method: 'GET'
            interceptor: {
                response: function(response) {      
                    var result = response.resource;        
                    result.$status = response.status;
                    return result;
                }
            }
        }                            
    });
    

    Usage:

    resource.get(params, function(result) {
        console.log(result.$status)
    });
    

    IMO status code should have been provided by default. There is an issue for this https://github.com/angular/angular.js/issues/8341

提交回复
热议问题