#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)
Change History (6)
comment:1 Changed 13 years ago by
comment:2 Changed 13 years ago by
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.
comment:4 Changed 13 years ago by
Milestone: | 1.3.2 → 1.4.2 |
---|---|
Resolution: | → fixed |
Status: | new → closed |
Version: | 1.3.2 → 1.4.2 |
comment:5 Changed 13 years ago by
Milestone: | 1.4.2 → 1.4.3 |
---|
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 :
I did a trace on the browsers differences in posts : Firefox 3.5.7
Chrome 4.0.249.7
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.