How to pass X-Auth-Token in the HTTP header in JavaScript

GibboK picture GibboK · Nov 12, 2013 · Viewed 39.3k times · Source

I have a website which uses Token-based authentication. At first, username and password are sent to log the user in and receive the token. For subsequent calls, I need to include the token as X-Auth-Token in HTTP header.

I would like to know how to do that, in vanilla JavaScript or using jQuery. Could you please provide me a sample of code?

Answer

asiviero picture asiviero · Nov 12, 2013

In jQuery it would be something like this:

$.ajax({
   url : myurl,
   headers: {
        'X-Auth-Token' : token
   });

More details on what you can do with $.ajax() are in the docs