Ticket #1623 (closed bug: worksforme)
$.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: | |
| Blocking: | Blocked by: |
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
Change History
Changed 5 years ago by davidserduke
-
attachment
jquery_test.html
added
comment:1 Changed 5 years ago by davidserduke
- Status changed from new to closed
- Resolution set to worksforme
- Milestone changed from 1.2.1 to 1.2.2
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

test case (built based on description)