doushang7209 2013-10-23 12:03
浏览 45
已采纳

检查ajax返回的json数据是否已设置

In my application im getting the data from database using ajax call as json array data. But some times i may not get the data from database based on condition. How can i check whether there is any data in the ajax returned array.

Here is my code..

Ajax Call:

   $.ajax({ 
        type:'POST',
        url:'user_panel/index',
        data: 'ov_prem_home_id='+home_id,
        dataType: 'json',
        cache: false,
        success: function(dataResponse){
        document.getElementById('ov_prem_title').value=data[0]['title'];
        }
    });

PHP Code

        $home_id=$_POST[home_id];   
        $ov_result=getPremOveriewData($home_id);
        echo json_encode($ov_result);exit;

I tried the conditions like isset(dataResponse),if(dataResponse=='') but i didn't got anything

  • 写回答

4条回答 默认 最新

  • dqd3690 2013-10-23 12:07
    关注

    If what you want is to check the data from the Javascript end, you could use something like:

    $.ajax({    
        type:'POST',
        url:'user_panel/index',
        data: 'ov_prem_home_id='+home_id,
        dataType: 'json',
        cache: false,
        success: function(dataResponse){
           if (data && dataResponse.length>0 && dataResponse[0]['title'])
           {
               document.getElementById('ov_prem_title').value=dataResponse[0]['title'];
           }
           else
           {
               //Empty
           }
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大