0

I send a request server-side by $resource which is inside my factory. In the return object there many information, but I'd like to have access to the authorization in the headers. I tried to print the returning object by console.log() but I dont see any headers and authorization in console. What should I do?

controllers.controller('ProfileSettingCtrl', function ($scope,User) {
     User.get({id: 'me'}, function(res) {
            console.log(res);
            $scope.profile = res;
        })

enter image description here

2 Answers 2

1

Documentation for $resource

Success callback is called with (value, responseHeaders) arguments.

Seems like you can just get the headers with function (res, headers) { console.log(headers); }

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

6 Comments

thanks. the header argument was good. but when I run console.log(headers). It doenst return a function like this function (name) { if (!headersObj) headersObj = parseHeaders(headers); if (name) { var value = headersObj[lowercase(name)]; if (value === void 0) { value = null; } return value; } return headersObj; }
@AliSaberi why would you expect it to return a function?
I also add photo to my post to make my explanations more clear.
@AliSaberi try responseHeaders('Authorization')
it gave me null! I doentt know why? as it is on photo, it should exist
|
0

According to the $resource docs, the header is passed as second argument to your success callback.

It's worth noting that the success callback for get, query and other methods gets passed in the response that came from the server as well as $http header getter function, so one could rewrite the above example and get access to http headers as:

var User = $resource('/user/:userId', {userId:'@id'}); User.get({userId:123}, function(u, getResponseHeaders){ u.abc = true; u.$save(function(u, putResponseHeaders) { //u => saved user object //putResponseHeaders => $http header getter }); });

If you want to use the header information, you have to use the success callback. Otherwise I always advice to use promises, which you can chain and pass around: User.get().$promise.then(successCallback).catch(errorCallback);

Comments

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.