spaceman spaceman - 1 month ago 27
Ajax Question

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);
}

Answer

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.