Skip to main content

Bug Tracker

Side navigation

#5123 closed bug (fixed)

Opened August 25, 2009 03:31PM UTC

Closed June 15, 2010 03:09AM UTC

Last modified March 10, 2012 06:59AM UTC

Chrome $.ajax doesn't work without data option

Reported by: coxmatthew Owned by:
Priority: minor Milestone: 1.4.3
Component: ajax Version: 1.4.2
Keywords: ajax chrome Cc:
Blocked by: Blocking:
Description

Found that my ajax processing wasn't working in chrome. Although the same request using $.post() worked fine.

Workaround:

adding an empty data attribute (data: "") seems to work around this issue.

Details:

----------------------------

Succeeds

----------------------------

$.post("test.php",

function(data){

alert("Test results: " + data);

},

"text");

--------------------------------

Throws error in chrome (ver 2.0.172.39)

----------------------------------

jQuery.ajax({

type: "POST",

url: "test.php",

success: function(data){

alert("Test results: " + data);

},

error: function (XMLHttpRequest, textStatus, errorThrown) {

alert("Error with ajax Function: "+ textStatus+" "+errorThrown);

},

dataType: "text"

});

----------------------------

Succeeds

//----------------------------

jQuery.ajax({

type: "POST",

url: "test.php",

data: "",

success: function(data){

alert("Test results: " + data);

},

error: function (XMLHttpRequest, textStatus, errorThrown) {

alert("Error with ajax Function: "+ textStatus+" "+errorThrown);

},

dataType: "text"

});

Attachments (1)
Change History (5)

Changed February 11, 2010 11:40PM UTC by benscript comment:1

Seeing the same issue in Chrome 4.0.249.78 (36714). It handling without a data option present seems to make the calls fail. The browser error is really pretty random :

ReferenceError
arguments: Array
message: "doIt is not defined"
stack: "ReferenceError: doIt is not defined at chrome-extension://oangcciaeihlfmhppegpdceadpfaoclj/js/co…"
type: "not_defined"

I did a trace on the browsers differences in posts :

Firefox 3.5.7

POST /channel/list?format=json HTTP/1.1
Host: 192.168.217.117:3000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6
Accept: application/json, text/javascript, */*
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://192.168.217.117:3000/channel/configuration
Cookie:****
Pragma: no-cache
Cache-Control: no-cache
Content-Length: 0

Chrome 4.0.249.7

POST /station/list?format=json HTTP/1.1
Host: 192.168.217.117:3000
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.78 Safari/532.5
Referer: http://192.168.217.117:3000/station/configuration
Content-Length: 9
Cache-Control: max-age=0
Origin: http://192.168.217.117:3000
X-Requested-With: XMLHttpRequest
Content-Type: application/xml
Accept: application/json, text/javascript, */*
Accept-Encoding: gzip,deflate,sdch
Cookie: ****
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Seems Chrome is getting the ''Content-Type'' set as application/xml. I'm not sure if this is a jquery issue or chrome. Looking at the actual xml is the word 'undefined', not valid XML-which is probably causing the server to fail and send back the failed response-or at least this was the situation in my case.

We just used GETs in our situation as a POST wasn't needed.

Changed February 15, 2010 09:56PM UTC by aniero comment:2

The real problem is that the XMLHttpRequest.send function on Chrome passes 'undefined' over the wire when given an undefined value instead of an empty string like Firefox/Safari/etc. There is an open ticket on the chromium project that describes this behavior: Issue 33062: XMLHttpRequest.send sends 'undefined' string when passed undefined value

In jQuery, the

jQuery.ajax
function passes in
jQuery.ajaxSettings.data
even when it's undefined. The final call to xhr.send is on ajax.js line 482.

I can bypass this problem by setting

jQuery.ajaxOptions.data = null
on page load.

But the problem is subtle: rails param parsing just blows up without explanation. I suspect the best fix will be simply setting

jQuery.ajaxSettings.data
to
null
instead of leaving it undefined as on line 173 of ajax.js.

Changed March 30, 2010 09:51PM UTC by mislav comment:3

This bug bit us today. Patch with test attached.

Changed June 15, 2010 03:09AM UTC by john comment:4

milestone: 1.3.21.4.2
resolution: → fixed
status: newclosed
version: 1.3.21.4.2

Changed June 15, 2010 03:09AM UTC by john comment:5

milestone: 1.4.21.4.3