0

How do I set a common request header using only JavaScript for every single HTTP call from the client side?

JAL
  • 41,701
  • 23
  • 172
  • 300

1 Answers1

0

I don't think there is a native support for this. But on the other hand you can extend the XMLHttpRequest class so it passes your custom header and override the existing XMLHttpRequest in the window.

Take a look at this answer : https://stackoverflow.com/a/9701050

Sample:

!(send){
    XMLHttpRequest.prototype.send = function (data) {
        this.setRequestHeader("X-Requested-With", "XMLHttpRequest"); // set your header here
        send.call(this, data);
    }
}(XMLHttpRequest.prototype.send);
Community
  • 1
  • 1
James
  • 2,756
  • 17
  • 19