How to handle empty return from getJSON

Posted by Gee on Stack Overflow See other posts from Stack Overflow or by Gee
Published on 2010-06-13T18:30:56Z Indexed on 2010/06/13 20:02 UTC
Read the original article Hit count: 358

Filed under:
|
|

Alright so I have a php script which gets results from a DB, and to get those results I'm using a jQuery script to pull the results via getJSON. It works perfectly but now I want to do something if the php script returns no results (empty).

I tried:

$.getJSON('path/to/script'), {parameter:parameter}, function(data){
 if (data) {
  alert('Result');
 } else {
  alert('Empty);
 }
});

But it's no good. I've tried different things like if (data.length) but still nothing. I've noticed that if there is no returned data the callback will never fire at all. So if that's the case, how do I handle a empty return?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery