dpqg86714 2017-10-24 04:48
浏览 46

为什么我的脚本忽略空数组结果?

I have a script that basically check if there is a result from a query:

$.post('<?php echo site_url('xxx/yyy'); ?>',{data:no},function(result){
        if(!result){
            alert("No Data");
        }
        else{
            alert("Data retrieved");
        }
});

Why my IF alert Data retrieved when I get empty JSON array. I tried to do alert(result) with different data (the data which result is empty and result is true), but both data is alerting Data retrieved.

This is my model:

$this->db->select('*',FALSE);
    $this->db->from('t_penomoran tp');      
    $this->db->join('t_penomoran_detail t_pd', 'tp.nomor = t_pd.nomor');

    $this->db->where('tp.nomor',$nomor);

    $query = $this->db->get();
    return $query->result_array();

note:
For some reason when I do alert(result.length) with data that has no value, the result is 2. But when I do alert(result) with data that has no value the result is []

  • 写回答

3条回答 默认 最新

  • dongmao4486 2017-10-24 04:51
    关注

    Empty array is a truthy value, meaning in your if statement, it will evaluate to true and that is why you see the alert(). To fix it, use the length property:

    if(!result.length) alert('no data')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭