23

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);
    }
spaceman
  • 1,628
  • 1
  • 16
  • 19

8 Answers8

36

In the AJAX operation just add: async: false after datatype: "json", and that should solve your problem. Chrome has issue handling asynchronous calls.

fragilewindows
  • 1,394
  • 1
  • 15
  • 26
MSS
  • 379
  • 1
  • 3
  • 2
  • Thank you, this resolved my issue. Although oddly, I'd only seen the issue in one specific place. It hasn't been a consistent problem for me. – enobrev Nov 28 '11 at 14:19
  • 1
    It works for me. You were of great help. It saved me from lots of lost time. – radu florescu Jul 20 '12 at 11:19
  • a jquery plugin was using 'jsonp' as dataType. I changed to 'json' and it works. Chrome doesn't like jsonp apparently. – Peanut Aug 27 '12 at 17:08
  • 7
    Whats the issue with handling asynch calls? – JoshBerke Aug 30 '12 at 17:37
  • Bite by this again, it's odd cause sometimes it works fine but certain pages or certain calls just fail. Would love to know the issue – JoshBerke Dec 27 '13 at 22:37
13

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.

spaceman
  • 1,628
  • 1
  • 16
  • 19
  • This worked but I had to remove both the datatype and data arguments – ninjasense Aug 30 '12 at 17:49
  • Rather than post a new answer I figure I would just comment on this one. Hope that's ok? I had an issue with chrome for mobile for me it ended up being I had " function something(page=0){} " when it should have been " function something(page){} " lol dumb mistake but changing it took care of it. Darn php! It loaded fine on all other browsers before the change only chrome had issue. – Kyle Coots Sep 16 '15 at 03:16
11

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.

Krokador
  • 111
  • 1
  • 2
  • 3
    This worked for me too. To be specific, my solution was to comment out //response.Close(); after a call to response.Flush() and before a call to response.End() – PandaWood Mar 01 '11 at 05:55
  • 3
    Replacing Response.Close() with Response.End() fixed it! Thanks a lot! – Drakarah Apr 10 '12 at 07:29
7

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);
  }
}
spaceman
  • 1,628
  • 1
  • 16
  • 19
  • i'm having the same problem as you, and this solution seems silly to me. Please update us if you find any more information regarding this. thanks for sharing this workaround anyway. – Moe Salih Jan 07 '10 at 19:13
  • 4
    I ran in to this today, setting the data part to "" did the trick. Thanks! – jonnii Mar 07 '10 at 20:46
  • 2
    I also had this issue - data: "" was the key. – RJ Owen Apr 20 '10 at 23:16
  • only works if added alert in error.status 200, but in normal I have loop for 5 ajax calls 'async: false', chrome get busy and show killing/waiting page dialog, but does not update status line "$('#status').html('calling:' + counter);" in error function with result.status == 200 condition. – Riz Dec 30 '10 at 12:18
  • 1
    Had this issue with parsing an xml file. Adding data: "" to the call resolved it for me. Thank you! – aaandre May 31 '11 at 17:56
2

I just ran into this in Chrome (was fine in Firefox) and adding async: true to the ajax parameters solved it.

Sam Malayek
  • 3,595
  • 3
  • 30
  • 46
1

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 :)

Lobstrosity
  • 3,928
  • 29
  • 23
  • thanks for the idea, but once again chrome gives me the finger... i have made one of two other methods since yesterday and they work fine using the layout above.. so im trying to figure out what exactly is going wrong in this case. – spaceman Nov 17 '09 at 06:25
0

Try this for success :

success: function(response) { branchDetailsSuccess(response); },
Jimmeh
  • 2,834
  • 1
  • 22
  • 21
-1

Use POST method in servlet and change type:POST in $.ajax.

fragilewindows
  • 1,394
  • 1
  • 15
  • 26
NAC
  • 135
  • 2
  • 11