Pregunta

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;
});
¿Fue útil?

Solución

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.

Otros consejos

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';

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)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top