jQuery自动完成ajax请求不显示返回的数据

时间:2015-09-03 13:24:01

标签: javascript php jquery ajax codeigniter

当我在调试开发人员工具上看到ajax请求响应数据但数据未在文本框中呈现。数据包含一些特殊字符,如图所示。

响应功能有什么问题?我应该添加什么(比如utf-8编码)来显示特殊字符?

enter image description here

HTML:

<select name="selCat">
    <option>....</option>
</select>

<input class="col-3" type="text" id="txtPOI" name="txtPOI" />

jquery的:

$("#txtPOI").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: '<?php echo site_url("crowd/get_POIs") ?>',
                data: {cat: selectedCode, q: request.term},
                dataType: "json",
                type: "post",
                success: function(data) {
                    response(data);
                },
                fail : function ( jqXHR, textStatus, errorThrown ) {
                    console.log(jqXHR);
                    console.log(textStatus);
                    console.log(errorThrown);
                },minLength: 3
            });
        }
    });

控制器:

function get_POIs(){
        $cat = $this->input->post('cat');
        $q = $this->input->post('q');
        //echo $cat;

        if (isset($cat) && isset($q)){
            $cat = strtolower($cat);
            $q = strtolower($q);
            $data=$this->crowd->get_POIs($cat,$q);
            //echo "aa";
            $a_json = array();
            if(count($data) > 0){
                foreach ($data as $row){
                      $a_json_row["title"] = $row->title;
                      $a_json_row["contentid"] = $row->contentid;
                      $a_json_row["latitude"] = $row->latitude;
                      $a_json_row["longitude"] = $row->longitude;
                      array_push($a_json, $a_json_row);

                }
                echo json_encode($a_json);
            }   
        }

    }

型号:

function get_POIs($cat, $q){

    $this->db->DISTINCT();
    $this->db->select('title, a.contentid, latitude, longitude, address');
    $this->db->from('attraction a');
    $this->db->join('geographicdata g', 'a.contentid = g.contentid', 'left');
    $this->db->where('cat3 = "'.$cat.'"');
    $this->db->where('title like "%'.$q.'%"');
    $this->db->order_by('title','ASC');
    $query = $this->db->get()->result();
    //die(var_dump($query));
    //echo $this->db->get_compiled_select();
    return $query;
}

1 个答案:

答案 0 :(得分:2)

我设法通过修改success事件中的代码来解决这个问题。我是怎么做到的。

改变
success: function(data) {
                response(data);
            }

success: function(data) {

            response( $.map( data, function( item )
            {
                return{
                        label: item.title,
                        value: item.title,
                        contentid: item.contentid,
                        latitude: item.latitude,
                        longitude: item.longitude
                    }
            }));

 }