Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass request headers in a jQuery AJAX GET call

I am trying to pass request headers in an AJAX GET using jQuery. In the following block, "data" automatically passes the values in the querystring. Is there a way to pass that data in the request header instead ?

$.ajax({          url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",          data: { signature: authHeader },          type: "GET",          success: function() { alert('Success!' + authHeader); }       }); 

The following didn't work either

$.ajax({          url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",          beforeSend: { signature: authHeader },          async: false,                              type: "GET",                     success: function() { alert('Success!' + authHeader); }       }); 
like image 617
Cranialsurge Avatar asked Jul 15 '10 18:07

Cranialsurge


People also ask

How do I get a header from AJAX request?

To add a custom header to an individual request then just add the headers property: // Request with custom header $. ajax({ url: 'edureka/co', headers: { 'custom-header': 'some value' } }); To add a default header to every request then use $.

What is request 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.

How get data from AJAX call in jQuery?

ajax({ type: "POST", url: 'test. php', data: {"type":"check"}, success: function(response){ alert(response); } }); There can obviously be more key-val pairs in data. In this case your alert should read: "The type you posted is check".

Is AJAX request GET or POST?

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.


1 Answers

As of jQuery 1.5, there is a headers hash you can pass in as follows:

$.ajax({     url: "/test",     headers: {"X-Test-Header": "test-value"} }); 

From http://api.jquery.com/jQuery.ajax:

headers (added 1.5): A map of additional header key/value pairs to send along with the request. This setting is set before the beforeSend function is called; therefore, any values in the headers setting can be overwritten from within the beforeSend function.

like image 101
Lukas Avatar answered Sep 17 '22 15:09

Lukas