gpt4 book ai didi

javascript - jquery自动完成json值

转载 作者:行者123 更新时间:2023-12-03 11:20:44 24 4
gpt4 key购买 nike

我正在使用 jquery 自动完成功能和 json 请求。

我的自动完成代码是:

$("#company_name").autocomplete({
source: function( request, response ) {
$.ajax({
url: "https://url/Address.ashx?site=handelsregister",
dataType: "jsonp",
data: {q: request.term},
success: function(data) {
response($.map(data, function(item) {
return {
label: item.handelsnaam,
id: item.dossiernummer,
street: item.straat
};
}));
}
});
},
minLength: 2,
select: function(event, ui) {
$('#company_name').val(ui.item.label);
$('#street').val(ui.item.street);
}
});

响应看起来像这样:

{"resultatenHR":[{"handelsnaam": "Jan Tiebie B.V.", "dossiernummer": "35016711", "subdossiernummer": "0000", "vestigingsnummer": "000000408700", "straat": "Middelweg", "huisnummer": "65", "huisnummertoevoeging": "", "postcode": "1911EB", "plaats": "Uitgeest"},{"handelsnaam": "Jan de Wolf Beheer B.V.", "dossiernummer": "36018787", "subdossiernummer": "0000", "vestigingsnummer": "000020159137", "straat": "Peperstraat", "huisnummer": "17", "huisnummertoevoeging": "", "postcode": "1441BH", "plaats": "Purmerend"},{"handelsnaam": "Holding Jan Greijmans B.V.", "dossiernummer": "13014184", "subdossiernummer": "0000", "vestigingsnummer": "000010702075", "straat": "Kevelaerstraat", "huisnummer": "3", "huisnummertoevoeging": "", "postcode": "6002BL", "plaats": "Weert"},{"handelsnaam": "Holding Jan Prins B.V.", "dossiernummer": "34001716", "subdossiernummer": "0000", "vestigingsnummer": "000018682057", "straat": "Middenhavenstraat", "huisnummer": "72", "huisnummertoevoeging": "", "postcode": "1976CM", "plaats": "IJmuiden"},{"handelsnaam": "Jans Boven-Leeuwen B.V.", "dossiernummer": "52473201", "subdossiernummer": "0000", "vestigingsnummer": "000022396934", "straat": "Zwaanheuvelstraat", "huisnummer": "8", "huisnummertoevoeging": "", "postcode": "6657KJ", "plaats": "Boven-Leeuwen"},{"handelsnaam": "Jan Drost Holding B.V.", "dossiernummer": "56557876", "subdossiernummer": "0000", "vestigingsnummer": "000026099330", "straat": "Patrimoniumlaan", "huisnummer": "63", "huisnummertoevoeging": "", "postcode": "3904AB", "plaats": "Veenendaal"},{"handelsnaam": "Jan Bone Beheer B.V.", "dossiernummer": "09168070", "subdossiernummer": "0000", "vestigingsnummer": "000020131275", "straat": "C.A. Thiemestraat", "huisnummer": "60", "huisnummertoevoeging": "", "postcode": "6828DX", "plaats": "Arnhem"},{"handelsnaam": "Jan Berkers Beheer B.V.", "dossiernummer": "17150615", "subdossiernummer": "0000", "vestigingsnummer": "000005415055", "straat": "Griendtsveenseweg", "huisnummer": "26", "huisnummertoevoeging": "", "postcode": "5754AB", "plaats": "Deurne"},{"handelsnaam": "Jan Boudesteijn Holding B.V.", "dossiernummer": "34060251", "subdossiernummer": "0000", "vestigingsnummer": "000019124791", "straat": "Flevoland", "huisnummer": "1", "huisnummertoevoeging": "", "postcode": "1948RH", "plaats": "Beverwijk"},{"handelsnaam": "Jan van IJken Oude Bouwmaterialen B.V.", "dossiernummer": "32071283", "subdossiernummer": "0000", "vestigingsnummer": "000018555772", "straat": "Rozenbottelberg", "huisnummer": "1", "huisnummertoevoeging": "", "postcode": "3755BR", "plaats": "Eemnes"}]}

当我使用自动完成功能时,下拉列表显示但它是空的。

知道如何解决这个问题吗?

最佳答案

您的数组周围的 {"resultatenHR": 包装器给您带来了问题。您的 $.map 调用在此上下文中需要一个数组,而不是一个对象。

您需要从data中提取数组以传递给$.map()。看起来像这样:

        success: function(data) {
response($.map(data.resultatenHR, function(item) {
return {
label: item.handelsnaam,
id: item.dossiernummer,
street: item.straat
};
}));
}
});

关于javascript - jquery自动完成json值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27130486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com