How to tell if no data is returned from json jquery call
Im calling a url and getting json data back, If there is no开发者_如何转开发 data returned then I would like to be able to tell this with jquery. If there is no data to be return then the page that is called is blank.
Currently I have
var urlToGetJsonWith = '_Chapters.aspx?videoid=' + videoid;
jQuery.getJSON(urlToGetChaptersWith, function (data) {
}
But obviously if there is no data, then the call back function would not be executed.
Anybody know how to tell if no json was returned?
Cheers, Pete
You're assumption is wrong. The callback is fired anyway (except there was an error on the transmission itself).
So you could just check the data
object within your success handler.
if( data && data.something ) {
}
else {
// empty object
}
One thing to mention:
As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently.
精彩评论