【问题标题】:Not getting any AJAX response in Codeigniter 2.x在 Codeigniter 2.x 中没有得到任何 AJAX 响应
【发布时间】:2016-12-20 16:14:20
【问题描述】:

我正在使用 Codeigniter 2.x

我的控制器功能

function remove(){
    $id = $this->input->post("id");
    $res = $this->ask_model->remove($id);

    if($res){
        $data = array('msg' => 'Removed');
    } else {
        $data = array('msg' => 'Not removed');
    }
    echo json_encode($data);
}

注意:我写的是echo json_encode($data)

我的模型

function remove($inpt){
    $stat = array('stat' => 0);
    $this->db->where('id',$inpt);
    $this->db->update('uploads',$stat);

    if ($this->db->affected_rows() > 0) {
        return true;
    } else {
        return false;
    }

}

AJAX

$(".prv .rmv").click(function () {

    var rmval = $(this).attr('id');

    $.ajax({
        url: '<?php echo site_url("user-question-edit/remove"); ?>',
        data: {"id": rmval},
        dataType:'json',
        type: "post",
        success: function(data){
            console.log(data.msg);
         }
    });

});

在执行上述操作时,我的 MySQL 表正在更新,但 console.log 显示

POST http://path/to/user-question-edit/remove/net::ERR_CONTENT_DECODEDING_FAILED

状态显示失败。

我应该怎么做才能检查响应?

更新:我错误地输入了我的模型函数remove_upload

【问题讨论】:

    标签: jquery ajax codeigniter


    【解决方案1】:
    $config['compress_output'] = FALSE;
    And this worked for me, but the real solution should be inside apache configurations
    
    
    
    
    or you can do this
    $config['compress_output'] = TRUE;
    
     function reply(){
            $msg = $this->Message_model;        
            $insert = $msg->send_message2();
            header("Content-type: application/json; charset=utf-8");
    echo json_encode(array("status" => TRUE));
    die;
    
    
        }
    

    【讨论】:

      【解决方案2】:

      尝试关注希望你会得到帮助....

      function remove(){
          $id = $this->input->post("id");
          $res = $this->ask_model->remove($id);
      
          if($res){
              $data = array('msg' => 'Removed');
          } else {
              $data = array('msg' => 'Not removed');
          }
          echo json_encode($data);
      }
      

      并且在 Ajax 中使用 eval() 函数。所以请看下面的响应:

      $(".prv.rmv").click(function () { 
      
          var rmval = $(this).attr('id');
      
          $.ajax({
              url: '<?php echo site_url("user-question-edit/remove"); ?>',
              data: {'id': rmval},
              dataType:'JSON',
              type: 'POST',
              success: function(data){
                  var response=eval(data);
                  alert(response.msg);
               }
          });
      
      });
      

      【讨论】:

      • 我的模型函数名称是remove()。我打错了remove_upload
      • $(".prv,.rmv").click(function () { //multiple selector separated by comma 不是多重选择器。 .rmv.prv 的子 CSS 类。但是,如果我只将.rmv 放入$(".rmv"),它就可以正常工作。 我尝试输入eval,但它不起作用。
      • 您对提醒rmval 有价值吗?
      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 2014-04-10
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多