响应在ajax自动完成内无效

时间:2018-04-09 08:42:31

标签: javascript jquery ajax autocomplete

我正在使用自动填充插件http://jqueryui.com/autocomplete/#remote-jsonp

  $("#city" ).autocomplete({
     source: function( request, response ) {
      $.ajax({
        url: 'index.php?secController=studentProfile&action=employeeSearch',
        dataType: "JSON",
        data: {
        searchCriteria: request.term
        },
        success: function( data ) {
        console.log(data);
        response(data, function (item) {
        return {
            label: item.FulltName,
            value: item.id
        };
       });
      }
    });
  },
  minLength: 1,
  select: function( event, ui ) {
    log( ui.item ?
      "Selected: " + ui.item.label :
      "Nothing selected, input was " + this.value);
  },
  open: function() {
    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
  },
  close: function() {
    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
  }
});

控制台中的数据使用多个json数据(如

)获取
     0: {_id: {…}, FullName: "Aasiya Rashid Khan", FirstMiddle: "Aasiya Rashid", FirstLast: "Aasiya Khan", Employee: {…}}
     1:{_id: {…}, FullName: "Sana Jeelani Khan", FirstMiddle: "Sana Jeelani", FirstLast: "Sana Khan", Employee: {…}}
     2:{_id: {…}, FullName: "Asad Hussain Khan", FirstMiddle: "Asad Hussain", FirstLast: "Asad Khan", Employee: {…}}

员工的身份就像是“_id:{$ oid:”5aa75d8fd2ccda0fa0006187“}”

在上面的代码中,我试图将item.FullName作为自动完成的标签返回,并将_id作为值返回。他们没有工作。请帮忙!!!

1 个答案:

答案 0 :(得分:1)

response回调需要一个参数,在这里你提供了两个参数。我想您正在尝试将返回的JSON映射到所需的输出,请尝试:

response(
    data.map(item => ({
        label: item.FullName,
        value: item._id
    }))
)