Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery getJSON function

I am using the following code to get the json feed of a twitter users friends using the twitter api :

var url = "http://twitter.com/statuses/friends/"+twitter_handle+".json?callback=?";

//show ajax loading animation
$('#loading').show();

$.getJSON(url, function(data) {
   //hide ajax loading animation 
   $('#loading').hide();
   //Processing the JSON here
   //...
}); 

This works when the twitter handle is valid.But if it is invalid,i.e. when no such twitter user exists the callback function i defined is not executed, and the ajax loading animation does not get hidden.

So, is there a way i can determine in code whether the request for the json feed is failing,and then hide the loading animation ?

Thank You.

like image 716
rogerp Avatar asked Dec 29 '25 15:12

rogerp


1 Answers

the ccallback can return 2 arguments one of which is a textStatus that you can test against.

$.getJSON(url, function (data, textStatus) {
  // data will be a jsonObj
  // textStatus will be one of the following values: 
  //   "timeout","error","notmodified","success","parsererror"
  this; // the options for this ajax request
}

via: http://docs.jquery.com/Ajax/jQuery.getJSON

like image 52
easement Avatar answered Jan 01 '26 03:01

easement



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!