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?
You should check your PHP output to always output some data.
In my codes I add a success flag to my outputs, like this:
If I don’t output anything, I put false into success, which makes it easy to validate in jQuery via data.success:
Example:
If you don’t want to modify your output, you can setup an ajaxError to catch your reading problems.
Example:
P.S.: I dont know if the missing ‘ at the end of this line:
is really missing in your original, if so, your missing a ‘ 😉 >>