Bug Tracker

Opened 10 years ago

Closed 9 years ago

Last modified 8 years ago

#5123 closed bug (fixed)

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)

chrome-ajax-data.patch (1.8 KB) - added by mislav 10 years ago.
Chrome patch

Download all attachments as: .zip

Change History (6)

comment:1 Changed 10 years ago by benscript

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.

comment:2 Changed 10 years ago by aniero

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 10 years ago by mislav

Attachment: chrome-ajax-data.patch added

Chrome patch

comment:3 Changed 10 years ago by mislav

This bug bit us today. Patch with test attached.

comment:4 Changed 9 years ago by john

Milestone: 1.3.21.4.2
Resolution: fixed
Status: newclosed
Version: 1.3.21.4.2

comment:5 Changed 9 years ago by john

Milestone: 1.4.21.4.3
Note: See TracTickets for help on using tickets.