2

So, I'm trying to intercept the http calls to add the Authorization header on each call if exist. This works well exept of the fact that no matter which http method I use (GET, POST, DELETE) it send the request with OPTIONS method instead. What am I doing wrong?

Server is nodejs with restify.

angular.module('mymodule').factory('RequestService', function () {
    var token = null;

    var service = {
        setToken: function setToken(_token) {
            token = _token;
        },    

        getToken: function getToken() {
            return token;
        },

        request: function request(config) {
            if (token) {
                config.headers['Authorization'] = 'Token' + token;
            }
            return config;
        }

    }

    return service;
}).config(function($httpProvider) {
    $httpProvider.interceptors.push('RequestService');
}
4
  • Are your calls cross-domains calls ? Commented Jan 6, 2015 at 13:56
  • Indeed, but I use chrome plugin to allow CORS, it's working fine without the interceptor @SamuelEUSTACHI Commented Jan 6, 2015 at 13:58
  • which plugin Do you use ? Commented Jan 6, 2015 at 14:16
  • chrome.google.com/webstore/detail/allow-control-allow-origi/… Commented Jan 6, 2015 at 14:17

1 Answer 1

1

When you implement your own interceptor on Front-End side, you should remember about CORS:

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', req.headers.origin || "*");
    res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,HEAD,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'content-Type,x-requested-with,authorization123456,accept');
    next();
});

We should remember to set Acces-Controll-Allow on Methods as well as on Headers. In my case it's 'authorization123456' where I put my token which is added to every request from AngularJS.

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

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.