CodeIgniter没有从自动完成jquery接收帖子

时间:2014-12-12 10:36:22

标签: jquery codeigniter jquery-autocomplete

我必须花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);
    }
  }

模型     class 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 { 我试图发布它的图片,但由于声誉它不会让我

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:0)

你在脚本中有几个错误,首先我可以看到你没有回复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链接也有误,您已使用<script>使用<link>

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

答案 1 :(得分:0)

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

请改用

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

导致你提到的错误

答案 2 :(得分:0)

我终于找到了问题。 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);
    }
  }

}