2

I'm using angular and angular-resource version 1.1.5 and I'm using a $resource to make a request to a REST service. But it seems like the custom headers is not appended to the request. My definition is as below. Is there anything I did wrong?

myApp.factory('User', function($resource) {
    var User = $resource('http://localhost\\:7017/mydomain/users/jack', { }, {
        get: {
            method: 'GET',
            isArray: false,
            headers: {'X-Requested-By':'abc'}
        }
    });
    return User;
});

3 Answers 3

1

Read this to see how to configure default headers in one place: http://docs.angularjs.org/api/ng.$http

EDIT:

Your header must be included in Access-Control-Allow-Headers header in response to the OPTIONS request, which is sent automatically prior to your GET request.

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

10 Comments

Thanks for your reply. I'm using $resource not $http. ":" is a reserved character in $resource and I need to use double back slash.
Of course, you're right about the colon. Note however that $resource uses $http internally, so setting headers for $http will work for you. If not, then check this out: stackoverflow.com/questions/12791667/…
Yes. the "query" is a typo. It should be "get".
Thanks. I have read that thread. It says 1.1.3 support $resource headers. I tried both 1.1.3 and 1.1.5 but cannot get the custom header working.
How do you check if the header is sent? Look in the browser console. Maybe it's a server-side issue? BTW, I got it working even with Angular 1.0.8.
|
1

You can modify the default headers inside the $httpProvider.

the headers are an object and separated intocommon, patch, post and put

headers in dev tools

so if you want to change the default for all your requests, just do a

$httpProvider.defaults.headers.put['Content-Type'] = 'application/json';

Comments

1

You have to call get method by using its name, i.e User.get(callback)

It seems that custom headers do not get sent when get method is called with User.query(callback)

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.