How to send X-Auth-Token in request headers


#1

I’m desperate to fix the http request issue

Here’s my server, which works fine with curl

Accept: /
X-Auth-Token:2f6c72f2c244a01d9263ab1fa1c8b9b6c7544f9e445646ff98c356c8fb1264cb

< HTTP/1.1 401 Unauthorized
< Server: nginx/1.6.2
< Date: Tue, 14 Oct 2014 16:22:30 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< Cache-Control: no-cache
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
< Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With, X-Auth-Token
< Access-Control-Expose-Headers: X-Auth-Token
< Access-Control-Allow-Credentials: true
< Access-Control-Max-Age: 86400

But when I use angularjs, i got these

OPTIONS http://api.domain ionic.bundle.js:16271
XMLHttpRequest cannot load http://api.domain Invalid HTTP status code 500

this is my request function, which I implemented in service.js

var config = {
headers: {
‘X-Auth-Token’: token
},
cache: true
};
console.log(token);
//var storage = window.localStorage;
//window.localStorage.setItem(“key”, “value”);
var respose_json = function () {
var deferred = $q.defer();
$timeout(function () {
deferred.resolve(
$http.get(menu_url, config).success(function (result) {
//$ionicLoading.hide();
return result;
}));
}, 500);
return deferred.promise;
};

    return {
        all: respose_json,
        get: function (id) {
            return respose_json[id];
        }
    };

Help please!


#2

Same problem here!! Did you solve it?


#3

Somewhere in your code put this

$http.defaults.headers.common['X-Auth-Token'] = token;

So it will set globally :slight_smile:


#4

I tried it. Not working and the request get method changed to option.


#5

You almost there… normally when we make CORS, then it will try with OPTION first, if return from server allowing to make CORS then the real request would be made