Ticket #1079 (closed bug: worksforme)
FF and IE fail to send custom headers
| Reported by: | spinal007 | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.1.4 |
| Component: | ajax | Version: | 1.1.3 |
| Keywords: | ajax custom header headers XMLHttpRequest X-Requested-With | Cc: | |
| Blocking: | Blocked by: |
Description (last modified by john) (diff)
The 'X-Requested-With' header which should be built-in to jQuery is not being sent with the ajax request. I've checked every line of the code and I'm certain the following lines get executed:
1998 Set header so the called script knows that it's an XMLHttpRequest 1999 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
I've also tried my own implementation... Add custom header to every call (HTTP_X_METHOD:Ajax) $().ajaxSend(function(a,r,o){ r.setRequestHeader("X-Method", "Ajax"); });
But this also fails...
Attachments
Change History
Changed 6 years ago by spinal007
-
attachment
jquery-latest.js
added
comment:1 Changed 6 years ago by spinal007
I've now tried the following:
var customHeaders = function(r){
//var h = {'a':1, 'b':2, 'c':3, 'd':4,'e':'HI!'};
var h = {"X-Method":"Ajax"};
jQuery.each(h, function(i){
//$.log('r.setRequestHeader("'+ i +'", "'+ this + '");');
r.setRequestHeader(i, this);
});
}
// METHOD 1: ajaxSend
$().ajaxSend(function(a,r,o){ customHeaders(r); });
// METHOD 2: ajaxSetup, beforeSend
$.ajaxSetup({ beforeSend:function(r,o){ customHeaders(r); } });
I've verified that the code within function customHeaders does execute during runtime. No errors are reported but the headers are not included in the request.
comment:2 Changed 6 years ago by spinal007
It seems to work when I use ajaxSetup to set global to true.
However,
- the documentation says this option should be true by default
- xml.setRequestHeader("X-Requested-With", "XMLHttpRequest"); executes even if global is false, so the X-Requested-With header should have been sent.
- I custom headers set via ajaxSend should still have worked.
This is the patch I'm using... (Note: I use the custom header 'X-Method', but this can be used for any other custom headers)
// Ajax default settings
$.ajaxSetup({
global:true,
type:'GET',
headers: {"X-Method":"Ajax"},
beforeSend:function(r,o){ },
data:{}
});
// Add custom headers before every call (HTTP_X_METHOD:Ajax)
$().ajaxSend(function(a,r,o){
jQuery.each(
(o.headers || {}),
function(i){
r.setRequestHeader(i, this);
}
);
});
comment:3 Changed 6 years ago by malsup
Do you have a sample page which demonstrates this? If you look at the jQuery source code you will see that the X-Requested-With request header is *always* set. How are you confirming its absence?
comment:4 Changed 6 years ago by spinal007
I confirmed its absence using a simple by calling a simple ASP script via Ajax that prints all headers sent to the server.
Since reporting this behaviour I have downloaded the latest release of jQuery and I can no longer re-create it, which leads me to the assumption that a) it's been fixed, or b) it was an error with my code and I've since fixed it.
Either way, everything is fine now...
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

jQuery v1.1.1 (uncompressed)