dongshi9407 2013-05-06 10:59
浏览 37
已采纳

如果数据库中的数据不匹配,Codeigniter Ajax不会返回任何错误消息

I am working with AJAX in CodeIgniter.

Here is my code. This is working when my SQL query returns a result. If the query is empty, AJAX doesn't return anything.

My Model:

function view_filter_by_cat($id){
$sql = "SELECT * FROM ".TBL_FILTER_OPTION." WHERE FID=?";
$query=$this->db->query($sql,$id);
if($query->num_rows()){
    foreach ($query->result() as $row){
        $result[] = $row;
    }           
    $query->free_result();
    return $result;         
}

AJAX Controller:

public function find_filters_options(){
    if($this->input->post('FID')){
        $fid = $this->input->post('FID');
        $filterList= $this->filter_option_model->view_filter_by_cat($fid);          
        if($this->filter_option_model->view_filter_by_cat($fid)){               
            echo (json_encode($filterList));
        }else{              
            echo '0';
        }
    }
}

Ajax calling:

$.ajax({
    type: "POST",
    url: '<?php echo site_url('admin/products/find_filters_options'); ?>',
    data: {
        <?php echo $this->security->get_csrf_token_name(); ?> : '<?php echo $this->security->get_csrf_hash(); ?>',
        FID: fid
    },
    success: function(data1){
        alert(data1);
    }
});

My question is when the query doesn't return any value, I am unable to access the success message. I want to get the value '0', when the return is empty.

  • 写回答

1条回答 默认 最新

  • dongyu1614 2013-05-06 11:07
    关注

    you could change you controller to like:

    public function find_filters_options(){
      $result = array();
        if($this->input->post('FID')){
            $fid = $this->input->post('FID');
            $filterList= $this->filter_option_model->view_filter_by_cat($fid);          
            if($this->filter_option_model->view_filter_by_cat($fid)){               
                $result["got_data"] = "yes";
                $result["data"] = $filterList;            
            }else{              
                $result["got_data"] = "no";
            }
        }
        else {
          $result["got_data"] = "no";
        }
        echo (json_encode($result));
    }
    

    And your jquery ajax

    $.ajax({
      type: "POST",
      url: '<?php echo site_url('admin/products/find_filters_options'); ?>',
      data : {<?php echo $this->security->get_csrf_token_name(); ?> : '<?php echo 
    $this->security->get_csrf_hash(); ?>', FID : fid },
      dataType: "json",
      success: function(data1) {
        if(data1.got_data == "yes") {
            var data = data1.data; //get data from server here
        } 
        else {
           //dont have any data
           //show some appropriate message
        }
      }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧