Jquery自动完成不会给出任何结果

时间:2017-08-02 09:13:17

标签: php jquery codeigniter autocomplete

我是codeignitor的新手并试图从数据库中获取jquery autocomplete。关于这个话题已经有很多问题,但没有一个对我有帮助。

这是我在视图文件中的脚本功能

查看      

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js">
</script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js">
</script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-
 ui.css" />
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js">
 </script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>



<script type="text/javascript">
     $(function() {
        $( "#vname" ).autocomplete({
    source: function(request, response) {

        $.ajax({
        url: "vendor/search",
        data: { term: $("#vname").val()},
        dataType: "json",
        type: "GET",
        success: function(data){
           var resp = $.map(data,function(obj){
                return obj.tag;
           }); 

           response(resp);
        }
    });
},
minLength: 2
  });
     });
  </script>
   </head>
    .
    .
    .
    .
  <input type="text" class="form-control" name="vendor_name" id="vname" />

这里是控制器(供应商)功能(搜索),我试图从数据库中获取建议数组

控制器

public function search()
{

$json = [];

    $this->load->database();
    if(!empty($this->input->get("term"))){
        $this->db->like('name', $this->input->get("term"));
        $query = $this->db->select('id,name as text')
                    ->limit(10)
                    ->get("vendors");
        $json = $query->result();
    }
    echo json_encode($json);        
}

问题是,当我输入输入字段时,没有任何事情发生(没有自动完成显示)但我的供应商/搜索功能在直接访问它并传递某些内容作为参数时工作正常。我认为$_GET[term]总是空的或者什么。

我现在不知道该怎么办,任何建议都会让人感激不尽。

2 个答案:

答案 0 :(得分:0)

更改控制器

// Controller

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [((CustomCell*)cell) animate];
    });
}

替换脚本标记

public function search(){
    $json = [];
    $this->load->database();
    if(!empty($this->input->get("term"))){
        $this->db->like('name', $this->input->get("term"));
        $query = $this->db->select('id,name as text')
                    ->limit(10)
                    ->get("vendors");
        $json = $query->result();
    }
    //set page header
    $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($json));   
}

答案 1 :(得分:0)

更改此行:

url: "vendor/search",

为:

url: "<?php echo base_url('vendor/search'); ?>",