2

我正在尝试使用 jqueryUi 进行自动完成,但无法将数据绑定到它。

$("#CustomerName").autocomplete({
        source:
       function () {
           $.ajax({
               url: "SearchCustomer?key=" + $("#CustomerName").val(),
               async: false,
               dataType:"json",
               success: function (data) {
                   return data.ResultList;
               }
           })
       },
        minLength: 0, autoFocus: true, delay: 1000
    });

我的ajax结果是 -

{"Message":null,"Successfull":false,"Id":0,"Result":null,"ResultList":["Customer 2","Kohl\u0027s Corp","Test Corp"]}

如果我使用它,那么它工作正常

$("#CustomerName").autocomplete({
            source:["Customer 2","Kohl\u0027s Corp","Test Corp"],
            minLength: 0, autoFocus: true, delay: 1000
        });

提前致谢 !

4

1 回答 1

3

$.ajax 没有问题,您能否将返回的数据与响应包装起来,例如 response(data.ResultList);

$("#CustomerName").autocomplete({
    source: function( request, response ) {
            $.ajax({
               url: "SearchCustomer?key=" + $("#CustomerName").val(),
               dataType : "json",
               success: function (data) {
                   response(data.ResultList);
               }
           });
    },
    minLength: 0, 
    autoFocus: true, 
    delay: 1000
}); 
于 2013-04-08T11:23:50.407 回答