未捕获的TypeError:this.source不是函数

时间:2017-06-15 21:14:47

标签: ajax jquery-ui

我想预先安排所有客户,并在需要时将其提供给自动填充功能。我抓住了这个错误。整个应用程序中的组搜索是全局的。



<script>
     $(document).ready(function() {
         var customers = $.ajax({
             url: "http://localhost:8081/customers/all",
             type: 'GET',
             dataType: 'json',
             success: function (data) {
                 $.map(data, function (v, i) {
                     return {
                         number: v.number,
                         name : v.name,
                     };
                 });
             }
         });

         $("#customer-search").autocomplete({
             minLength: 0,
             source: customers,
             select: function( event, ui ) {
                 $( "#group-search" ).val(ui.item.name);
                 $("#group-search-form").submit();
                 return false;
             },
         }).autocomplete( "instance" )._renderItem = function( ul, item ) {
             return $( "<li>" )
                 .append( "<div>" + item.name + " " + item.number + "</div>" )
                 .appendTo( ul );
         };





     });
 </script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

在ajax请求完成后初始化自动完成,如下所示:

试试这个:

$(document).ready(function() {
 var customers = [];
 $.ajax({
     url: "http://localhost:8081/customers/all",
     type: 'GET',
     dataType: 'json',
     success: function (data) {
           $.map(data, function (v, i) {
             return {
                 number: v.number,
                 name : v.name,
             };
         });

         $("#customer-search").autocomplete({
             minLength: 0,
             source: customers,
             select: function( event, ui ) {
                 $( "#group-search" ).val(ui.item.name);
                 $("#group-search-form").submit();
                 return false;
             },
         }).autocomplete( "instance" )._renderItem = function( ul, item ) {
             return $( "<li>" )
                 .append( "<div>" + item.name + " " + item.number + "</div>" )
                 .appendTo( ul );
         }; 
     }
 });



});