Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a custom HTTP header to ajax request with js or jQuery?

Does anyone know how to add or create a custom HTTP header using JavaScript or jQuery?

like image 675
Txugo Avatar asked Oct 07 '11 11:10

Txugo


People also ask

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

What is header in Ajax?

The headers are additional key-value pairs send along with ajax request using the XMLHttpRequest object. An asynchronous HTTP request to the server by using The ajax() function and by including the header it describes to the server what kind of response it accept.

Which method is used to create HTTP request using JavaScript in Ajax?

To make an HTTP call in Ajax, you need to initialize a new XMLHttpRequest() method, specify the URL endpoint and HTTP method (in this case GET). Finally, we use the open() method to tie the HTTP method and URL endpoint together and call the send() method to fire off the request.


2 Answers

There are several solutions depending on what you need...

If you want to add a custom header (or set of headers) to an individual request then just add the headers property:

// Request with custom header $.ajax({     url: 'foo/bar',     headers: { 'x-my-custom-header': 'some value' } }); 

If you want to add a default header (or set of headers) to every request then use $.ajaxSetup():

$.ajaxSetup({     headers: { 'x-my-custom-header': 'some value' } });  // Sends your custom header $.ajax({ url: 'foo/bar' });  // Overwrites the default header with a new header $.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } }); 

If you want to add a header (or set of headers) to every request then use the beforeSend hook with $.ajaxSetup():

$.ajaxSetup({     beforeSend: function(xhr) {         xhr.setRequestHeader('x-my-custom-header', 'some value');     } });  // Sends your custom header $.ajax({ url: 'foo/bar' });  // Sends both custom headers $.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } }); 

Edit (more info): One thing to be aware of is that with ajaxSetup you can only define one set of default headers and you can only define one beforeSend. If you call ajaxSetup multiple times, only the last set of headers will be sent and only the last before-send callback will execute.

like image 144
Prestaul Avatar answered Oct 12 '22 12:10

Prestaul


Or, if you want to send the custom header for every future request, then you could use the following:

$.ajaxSetup({     headers: { "CustomHeader": "myValue" } }); 

This way every future ajax request will contain the custom header, unless explicitly overridden by the options of the request. You can find more info on ajaxSetup here

like image 35
Szilard Muzsi Avatar answered Oct 12 '22 10:10

Szilard Muzsi