0

I am new to AngularJs and I am trying to really wrap my mind on how to use $resource to get the HTTP status code that was passed back I have this factory I have setup with one way I have tried to implement getting the HTTP header with no success.

app.js

.factory("Resource", function ($resource, baseUrl) {
            return $resource(baseUrl + "rest/sessionId",
                {},
                {getSessionId: {method: "GET"}},
                get:
            {
                method: 'GET'
                interceptor: {
                    response: function (response) {
                        var result = response.resource;
                        result.$status = response.status;
                        return result;
                    }
                }
            }
            );
        })
1
  • do you need the header or the status code? Commented Apr 26, 2016 at 14:00

1 Answer 1

2

If you are new to Angularjs, I suggest you to use the $http Service:

https://docs.angularjs.org/api/ng/service/$http

// Simple GET request example:
$http({
  method: 'GET',
  header:{"content-type":"text"},
  url: '/baseUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available

          console.log(response.status)

  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

Otherwise you need to use an interceptor, but to my point of view is currently and overkill:

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

return resource;
});

you can return it from the factory.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately it is already setup wit all of my factories to use $resource.... however the interceptor is not a bad idea. How would you apply it to my code.
can i just add the interceptor to the factory? I really dont know how to apply to my situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.