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

前端 未结 8 1816
半阙折子戏
半阙折子戏 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条回答
  •  Happy的楠姐
    2020-12-05 03:29

    I think the right answer is a combination of Bardiel's and Ara's answers.

    After adding 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;
                }
            }
        }                            
    });
    

    Use it as below:

    RestAPI.save()
    .query(function(response) {
        // This will return status code from API like 200, 201 etc
        console.log(response.$status);
    })
    .$promise.catch(function(response) {
        // This will return status code from server side like 404, 500 etc
        console.log(response.status);
    });
    

提交回复
热议问题