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); } });
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 $.
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.
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".
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With