自动完成:未捕获的TypeError:无法使用'in'运算符来搜索'length'

问题描述 投票:2回答:2

错误

“未捕获的TypeError:无法使用'in'运算符来搜索”length“

正在谷歌Chrome控制台中显示

$(document).ready(function() {
  $("#trackerid").change(function() {
    var var_locoid = $("#trackerid option:selected").val();
    //  alert(var_locoid);
    $("#deliverylocation").autocomplete({

      source: function(request, response) {
        var auto_data = $("#deliverylocation").val();
        // alert(auto_data);

        //alert(var_locoid);
        $.ajax({
          url: "http://localhost/CodeProject/Testctrl/lookup",
          type: "POST",
          datatype: "json",
          //returnType:"json",
          data: {
            'var_locoid': var_locoid,
            'auto_data': auto_data
          },
          success: function(data) {
            var resp = $.map(data, function(obj) {
              return obj.tag;
            });

            response(resp);
          }
        });
      },
      minLength: 1
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
javascript jquery codeigniter
2个回答
4
投票

试试这个

jQuery.parseJSON(data); 

0
投票

in运算符仅适用于对象。您正在使用字符串。在使用$ .map之前,请确保您的值是一个对象。在这种特定情况下,您必须解析JSON:

$.map(JSON.parse(data), ...);
© www.soinside.com 2019 - 2024. All rights reserved.