1

我有一个脚本,我通过 JSON 获取一些数据,这就像:

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

当我收到一些数据时它工作得很好,但是当它为空时,我得到下一个错误:

未捕获的类型错误:无法读取 null 的属性“长度”

我得到这个错误的地方是这样的一行:

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

我想要的是控制 JSON 何时发送数据,我该怎么做?

编辑:当数据是空对象,当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 回答