javascript 实现自动完成时出现“this.source 不是函数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14598743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:03:51  来源:igfitidea点击:

"this.source is not a function" error while implementing autocomplete

javascriptjsonjqueryjavascript-events

提问by Tieson T.

$(document).ready(function(){
   var var_name=null;
   $('#id1').click(function(){

      $.ajax({
         type:"GET",
         url:" ggs.erm.servlet.setup5.Page",
         success:function(response){
            var_name=response;
            console.log(response);
         }
      })
   });
   $("#id").autocomplete({source:var_name});
});


This is the Code I am messing with,It says TypeError:this.source is not a function. Where I am wrong,Correct me???screenshot of Error and Json from response


这是我弄乱的代码,它说 TypeError:this.source 不是函数。我哪里错了,纠正我???响应中错误和 Json 的屏幕截图

回答by Tieson T.

jQuery Ajax methods are non-blocking, so it looks like you're trying to set an auto-complete source before the previous method resolves. You probably want to move the autocompleteassignment into the success method of your .ajax()call.

jQuery Ajax 方法是非阻塞的,因此看起来您正在尝试在前一个方法解析之前设置自动完成源。您可能希望将autocomplete分配移动到您.ajax()调用的成功方法中。

So, instead of what you have, use:

所以,而不是你所拥有的,使用:

$.ajax({
    type:       "GET",
    url:        "ggs.erm.servlet.setup5.Page",
    success:    function(response) {
        $("#id").autocomplete({ source: response });
    }
});