jquery ajax problem in chrome
i have the following jquery code running on my page just fine in FF and IE, but chrome seems to be freaking out..
in FF and IE the call is made and the result is appended to the div. in chrome, it calls ajaxfailed on failure.
the XMLHttpRequest passed to the AjaxFailed function has a status code of "200" and the statusText is "ok". the readystate is 4 and the responseText is set to the data i wish to append to the div.. basically from what i can see its calling the failure method but it isn't failing.. i have tried with both get and post requests and it always breaks in chrome.
function getBranchDetails(contactID, branchID) {
$.ajax({
type: "GET",
url: urlToRequestTo,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: branchDetailsSuccess,
error: AjaxFailed
});
}
function branchDetailsSuccess(result) {
$("#divBranchControl").empty();
$("#divBranchControl").append(" " + result);
$("#branchDiv").tabs();
}
function AjaxFailed(result) {
alert("FAILED : " + result.status + ' ' + result.statusText);
}
Solution 1:
In the AJAX operation just add: async: false
after datatype: "json"
, and that should solve your problem. Chrome has issue handling asynchronous calls.
Solution 2:
I just saw that this question has gotten a lot of views, and its still open. I'd forgotten about it completely and hopefully this will let me close it.
Setting the datatype argument to nothing, or even removing the datatype argument completely will solve the problem.
In my example I am returning a rendered view (an html snippet in string) and in this code I'm specifying the data type to json, when really it isn't. Most other browsers seem to ignore the datatype if its incorrect and move on with life, allowing me to append the html result.
Chrome throws an error. The status text is OK, the status code is 200, because the actual ajax request went through fine. The problem has nothing to do with the request itself, the problem is that the data returned is not what I told chrome it would be.
So chrome breaks. If I remove the datatype argument completely chrome figures out what the data is when it gets it. If I set the datatype argument to "html" then it also works fine.
Long story short, the problem is not chrome. Its me. Because I'm dumb like that. I am marking this as the answer to this question as it answers the example I presented in the original question.
In the comments others have described other situations where this solution is most likely not going to help.
Solution 3:
I don't know if you still have this issue, but I ran into a similar error just today. I was calling an aspx page to return a string with responseText and Chrome never returned anything. Turned out I had a Response.Close in my aspx page which worked everywhere else but probably didn't send some required headers or something to Chrome and/or Safari. Hope that helps someone.
Solution 4:
after a day and a half i got over it, so may i present.....
function getBranchDetails(contactID, branchID) {
$.ajax({
type: "GET",
url: urlToRequestTo,
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: branchDetailsSuccess,
error: branchAjaxFailed
});
}
function branchDetailsSuccess(result) {
$("#divBranchControl").empty();
$("#divBranchControl").append(" " + result);
$("#branchDiv").tabs();
}
function branchAjaxFailed(result) {
if (result.status == 200 && result.statusText == "OK") {
//this is here only because chrome breaks on this method only for no reason whatsoever.
//chrome sees the request as failed, but everything happens fine...
branchDetailsSuccess(result.responseText);
}
else {
alert("FAILED : " + result.status + ' ' + result.statusText);
}
}
Solution 5:
Try setting the data parameter to "".
For GET
requests, the data parameter is appended to the URL. Not sure why Chrome would have an issue with that but it's worth a shot :)