tags:

views:

42

answers:

3

Hello All,

I need some suggestions here or maybe some explanations. I have a jquery ajax call,

$.ajax({
 type: "GET",
 url: base_url+'/ajax/fetch/counts/',
 dataType: 'json',
 data: {},
 error: function(xhr, error){
        console.debug(xhr); console.debug(error);
 },
 success: display_counts
});

It's working fine. My success callback fires correctly with response. But, what I noticed is that my error callback is fired every time, even when my call returns success status 200. In the above error callback, I see that object xhr.status is 200.

Can anybody explain what's wrong, or what is happening here? error callback is supposed to fire only when I have 404 or maybe a non-200 response. Are my assumptions correct?

Thanks.

A: 

A recent question had similar problem with json jquery requests, try removing surrounding () from your json response.

aularon
Funny, I've had a similar problem that was fixed by *adding* surrounding brackets.
Coronatus
@Coronatus Yes, this what I usually used to do, but jquery 1.4.2 starts to rely on browser's `JSON.parse`, my firefox's `JSON.parse` would parse `{"key":"value"}` successfully, but issue an error on `({"key":"value"})`.
aularon
To get around the differences in which browsers parse JSON, I would recommend using Douglas Crockford's parser: http://www.json.org/json2.js
SimpleCoder
+1  A: 

A few things I can think of:

  1. Make sure you have disabled caching by setting cache: false.
  2. If you are using Firefox, try using Firebug and the Net tab to monitor the request
  3. Don't rely on the browser's JSON parser. I would recommend this one: http://www.json.org/json2.js from the creator of JSON no less
SimpleCoder
Well, i have already tried trivial and obvious doubts. :(
simplyharsh
"trivial and obvious" - well, that was simply harsh. :)
SimpleCoder
Well sorry if that was really harsh. +1 already for referring to JSON parser thing. :)
simplyharsh
just joking, no problem
SimpleCoder
A: 

I'm not a jQuery expert, but I know that bwith Prototype.js, the AJAX error handler fires if the request is successful but the success handler causes an an error. Is that the same in jQuery? You could test if this is what's happening by putting the entire contents of display_counts in a try..catch block.

Josh