【问题标题】:CodeIgniter not receiving post from autocomplete jqueryCodeIgniter 没有收到来自自动完成 jquery 的帖子
【发布时间】:2014-12-12 10:36:22
【问题描述】:

我必须花费 30 个小时来弄清楚这一点。 CodeIgniter 似乎没有接收到来自自动完成脚本的数据,我这辈子都不知道为什么。

查看

<head>
        <script src="https://localhost/testsite/assets/js/jquery/jquery-ui.js" type="text/javascript" ></script>    
        <script src="https://localhost/testsite/assets/js/jquery/jquery-ui.css" type="text/javascript" ></script>   
</head>
<input type="text" id="postcode" name="postcode" />


<script>

$(document).ready(function(){
    var postcode = $('#postcode').val();
    $("#postcode").autocomplete({
        source: "postcode/get_postcode", // path to the get_birds method
        dataType: 'json',
        type: 'post',
        data: { postcode: postcode}

  });
});
</script>

控制器

    $this->load->model('postcode_model');

    $postcode = $this->input->post('postcode');

    if(isset($postcode)){
        $this->postcode_model->get_postcode($postcode);
    }else{
        $data['error'] = $postcode;
            $this->load->view('error', $data);
    }
  }

型号 类 postcode_model 扩展 CI_Model {

  function get_postcode($q){

    $query = $this->db->query("SELECT postcode
                                   FROM geo 
                                   WHERE postcode LIKE '$q%' 
                                 ");     

    $row_set = array();

     if($query->num_rows > 0){
        foreach ($query->result_array() as $row){

            $row_set[] = $row['postcode']; 

        }

        echo json_encode($row_set); //format the array into json data
    }else{
        echo "error";
    }

    }
}

如果我向模型发送测试整数,脚本确实会正确抓取邮政编码列表,然后在 html 中显示它们,所以我似乎遇到的唯一问题实际上是自动完成以将文本字段条目发送到控制器

在萤火虫我得到这个错误: SyntaxError:语法错误 .ui-helper-hidden { 我试图发布它的图片,但由于声誉问题它不会让我发布

任何帮助将不胜感激

【问题讨论】:

  • 您的 JavaScript 代码中很可能存在语法错误,因此浏览器不会执行任何 JavaScript。
  • 没有语法错误,所有的 javascript 都在运行,除了模型没有接收到数据,URL 是正确的

标签: jquery codeigniter jquery-autocomplete


【解决方案1】:

问题出在您的 html 标头标记中,您将 .css 文件包含为 JavaScript

请改用这个

<link media="all" type="text/css" rel="stylesheet" href="https://localhost/ozjobs/assets/js/jquery/jquery-ui.css">

这是导致您提到的错误

【讨论】:

    【解决方案2】:

    你在脚本中有几个错误,首先我可以看到你没有返回响应json

     if(isset($postcode)){
        $this->postcode_model->get_postcode($postcode); // Here
    

    改成

    if(isset($postcode)){
        $this->output
          ->set_content_type('application/json')
          ->set_output( 
              json_encode( $this->postcode_model->get_postcode($postcode) 
          ));
    

    在 JS 中

     data: { "postcode": postcode} // should be different you have assigned same as key and value
    

    而且css链接也错了,你用&lt;script&gt;&lt;link&gt;

     <link type="text/css" rel="stylesheet" href="https://localhost/ozjobs/assets/js/jquery/jquery-ui.css">
    

    【讨论】:

    • 我没有发布我将在一秒钟内发布的模型,它正在模型中编码并在 foreach 语句中回显
    • echo in model 是个坏主意,它会限制你重复使用
    【解决方案3】:

    我找到了问题……终于。 1. 数据{} 的名称需要与您的字段 ID (#postcode) 不同 2. 自动完成不喜欢 POST 所以使用 GET

    查看

    <input type="text" id="postcode" name="postcode" />
    
    
    <script>
    $(document).ready(function() {
    
      $("#postcode").autocomplete({
        source: "<?php echo base_url('postcode/get_postcode')?>",
        dataType: 'json',
            type: 'get',
            data: { term: $("#postcode").val()},        
      });
    
    });
    </script>
    

    控制器

      function get_postcode(){
    
        $this->load->model('postcode_model');
    
        $postcode = $this->input->get('term');
    
    
    
        if(isset($postcode)){
            $this->postcode_model->get_postcode($postcode);
        }else{
            $data['error'] = $postcode;
                $this->load->view('error', $data);
        }
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-19
      • 1970-01-01
      • 2019-03-29
      • 2014-07-10
      • 2012-12-27
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多