1

JSONを介してデータを取得するスクリプトがあります。これは次のようなものです。

$.getJSON('phpfile', function(data) { ...

一部のデータを受信すると完全に機能しますが、データが空の場合は次のエラーが発生します。

Uncaught TypeError:nullのプロパティ'length'を読み取れません

このエラーが発生するのは、次のような行です。

if(data==null){
 //actions if the JSON is doesnt give me data
}else{
 //actions if I get data
}

JSONがデータを送信するかどうかを制御したいのですが、どうすればよいですか?

編集:データがnullオブジェクトの場合、jsonがデータを取得しない場合、$。each(data、function(key、val){でエラーが発生します。

前もって感謝します

4

1 に答える 1

1
$.getJSON('phpfile', function handleSuccess(data) {
    if(!data){ 
          //handle if null or undefined
    }else{
         //actions if I get data
    }
    }).error(function handleErrorCase(){
        //handle error i.e. your server returning something other 200 or not json
    };
于 2012-10-21T19:15:14.830 に答える