Jquery 引导typeahead无法使用我的以下代码

Jquery 引导typeahead无法使用我的以下代码,jquery,twitter-bootstrap,bootstrap-typeahead,Jquery,Twitter Bootstrap,Bootstrap Typeahead,我有一个搜索地方的密码。服务器以json格式正确返回数据,但typeahead未显示结果 <script type="text/javascript"> $(document).ready(function(e) { $('#txt_ser').typeahead({ minLength:1, source: function (query, process) { var places = []; var map

我有一个搜索地方的密码。服务器以json格式正确返回数据,但typeahead未显示结果

<script type="text/javascript">
$(document).ready(function(e) {
    $('#txt_ser').typeahead({
      minLength:1,
      source: function (query, process) {
          var places = [];
          var map = {};
          $.ajax({
              dataType: "json",
              url: "<?php echo base_url() . "ajax/ser";?>",
              data: 'q='+query,
              type: 'POST',
              success: function (data) {
                  $.each(data, function(i, place){
                      map[place.yt_center_state] = place;
                      places.push(place.yt_center_state);
                  });
                  return process(places);
               }
          })
      }
    });
});
</script>

您没有将ajax请求的结果返回给typeahead
source
。这一行代码:

 return process(places);
$.ajax
success
处理程序中,因此它只将
位置
返回给该函数

您应该将代码更改为:

$('#txt_ser').typeahead({
  minLength:1,
  source: function (query, process) {
      var places = [];
      var map = {};
      return $.ajax({   // <- add return here
          dataType: "json",
          url: "<?php echo base_url() . "ajax/ser";?>",
          data: 'q='+query,
          type: 'POST',
          success: function (data) {
              $.each(data, function(i, place){
                  map[place.yt_center_state] = place;
                  places.push(place.yt_center_state);
              });
              return process(places);
          }
      });
  }
});
$('#txt_ser')。请提前键入({
最小长度:1,
来源:功能(查询、流程){
var位置=[];
var-map={};

return$.ajax({//)控制台中有错误吗?我对
的语法有一些疑问
但是我不是一个PHP玩家,而且是发送到服务器的ajax请求,成功回调被调用没有错误,我添加了服务器返回的json数据,状态代码为200。这个问题与PHPNo没有任何关系。更改代码后,它也不工作……服务器正在以json发送数据格式。我网站的这一部分真的伤害了我……嗯,如果你在返回过程(位置)之前执行
console.log(位置);
,你能看到你期望的
位置吗?否则我猜foreach循环有问题。console.log显示的结果像[“MH”、“MH”、“MH”、“MH”]那么我肯定你的问题是另外一个问题。用几乎相同的数据检查我的小提琴:
$('#txt_ser').typeahead({
  minLength:1,
  source: function (query, process) {
      var places = [];
      var map = {};
      return $.ajax({   // <- add return here
          dataType: "json",
          url: "<?php echo base_url() . "ajax/ser";?>",
          data: 'q='+query,
          type: 'POST',
          success: function (data) {
              $.each(data, function(i, place){
                  map[place.yt_center_state] = place;
                  places.push(place.yt_center_state);
              });
              return process(places);
          }
      });
  }
});