Bug Tracker

Opened 13 years ago

Closed 13 years ago

#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:
Blocked by: Blocking:

Description (last modified by john)

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 (1)

jquery-latest.js (58.5 KB) - added by spinal007 13 years ago.
jQuery v1.1.1 (uncompressed)

Download all attachments as: .zip

Change History (6)

Changed 13 years ago by spinal007

Attachment: jquery-latest.js added

jQuery v1.1.1 (uncompressed)

comment:1 Changed 13 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 13 years ago by spinal007

It seems to work when I use ajaxSetup to set global to true.

However,

  1. the documentation says this option should be true by default
  1. xml.setRequestHeader("X-Requested-With", "XMLHttpRequest"); executes even if global is false, so the X-Requested-With header should have been sent.
  1. 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 13 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 13 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...

comment:5 Changed 13 years ago by john

Description: modified (diff)
Milestone: 1.1.31.1.4
Resolution: worksforme
Status: newclosed
Version: 1.1.11.1.3
Note: See TracTickets for help on using tickets.