简体   繁体   English

在jquery的自动完成功能中得到错误“ this.source不是一个功能。”

[英]Getting the error in the autocomplete function of the jquery “this.source is not a function.”

My code is : 我的代码是:

        $.ajax({
            type: "GET",
            url: "https://url.com",
            dataType: "json",
            success: function (data) {

                //$("#id").autocomplete({ source: response });
                $("#search").autocomplete(
           {
               source: data,
               select: function (event, ui) {
                   $("#search").val(ui.item.FirstName + " / " + ui.item.LastName);
                   return false;
               }
           }).data("autocomplete")._renderItem = function (ul, item) {
               return $("<li></li>")
                   .data("item.autocomplete", item)
                   .append("<a><strong>" + item.FirstName + "</strong> / " + item.LastName + "</a>")
                   .appendTo(ul);
           };
            }
        });

The data in the url is of the form {{Data: Array[22], Id: null, Result: Object} 网址中的数据的格式为{{Data:Array [22],Id:null,结果:Object}

I want to use the FirstName and LastName of the Array which is of the form 我想使用形式为Array的FirstName和LastName

{FirstNam:R , LAstName:e , Id:9 },{FirstNam:R , LAstName:e , Id:9},...

I tried this also 我也尝试过

$.getJSON("https://url.com", function (json) {
                console.log(json);    

            });

But this also gives the same error. 但是,这也会产生相同的错误。

Any help ?? 有帮助吗??

I have tried this also 我也尝试过

  $("#search").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "https://url.com",
                dataType: "json",
                data: { term: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                         return {
                            label: item.FirstName,
                            id: item.LastName 

                        };
                    }));
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {
            $('#search').val(ui.item.label);
                      }
    });

this not even working 这甚至不起作用

Try data.d as source. 尝试使用data.d作为源。 Like this: 像这样:

$("#search").autocomplete({
   source: data.d,
});

just data is an object but data.d makes it into a readable string or something similar. 只是data是一个对象,而data.d使它成为可读的字符串或类似的东西。

Also, you shouldn't use the success function because it's been deprecated by jQuery since 1.8, instead use this: 另外,您不应该使用success函数,因为自1.8以来jQuery已弃用了success函数,请使用以下函数:

$.ajax({
  type: "GET",
  url: "https://url.com",
  dataType: "json"
}).done(function(data){
  console.log(data.d);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM