Skip to main content

Bug Tracker

Side navigation

#1623 closed bug (worksforme)

Opened September 15, 2007 05:58PM UTC

Closed December 04, 2007 06:47PM UTC

$.ajax not sending X-Requested-With with dataType script

Reported by: jeremydurham Owned by:
Priority: minor Milestone: 1.2.2
Component: core Version: 1.2
Keywords: xhr x-requested-with ajax Cc:
Blocked by: Blocking:
Description

I'm attempting to call the following code with jQuery 1.2:

$.ajax({async:true, dataType:'script', type:'POST', url:'/foo/bar'}); return false;

With jQuery 1.1.x, I was getting back an X-Requested-With of XMLHttpRequest; with jQuery 1.2 I do not get the X-Requested-With back. It seems to be because jQuery 1.2 injects a script tag into the header of the page, whereas jQuery 1.1.4 does not.

Ruby on Rails checks that the X-Requested-With header is set to determine if this is an XML Http request.

Attachments (2)
  • jquery_test.html (0.8 KB) - added by davidserduke December 04, 2007 06:41PM UTC.

    test case (built based on description)

  • load.js (0.0 KB) - added by davidserduke December 04, 2007 06:41PM UTC.

    test case data

Change History (1)

Changed December 04, 2007 06:47PM UTC by davidserduke comment:1

milestone: 1.2.11.2.2
resolution: → worksforme
status: newclosed

This may or may not have been a problem in 1.2, but when tested in 1.2.1 it works as expected. Using the above test case with FireBug, I found the X-Requested-With was set

Request Headers
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11
Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer http://localhost/jquery/test/1623/jquery_test.html
Pragma no-cache
Cache-Control no-cache

If it is a cross domain script load (i.e. the url starts with "http") then yes, that will not be set since XMLHttpRequest won't be used but rather the script is inserted in the head element.