简体   繁体   English

自动完成不显示字符串数组

[英]autocomplete not display string array

What I am trying to achieve: When a user finishes typing in their postcode autocomplete will show all of the possible addresses that are valid. 我要达到的目的:当用户完成邮政编码输入时,自动完成功能将显示所有可能的有效地址。

How I am trying to do this: As the user enters their postcode, 6 or 7 characters, after a second delay in their typing, autocomplete will call the AJAX function which will return the JSON, which will be translated for autocomplete to display client side. 我如何尝试执行此操作:当用户输入其邮政编码(6或7个字符)时,在键入延迟一秒钟后,自动完成功能将调用AJAX函数,该函数将返回JSON,该JSON将转换为自动完成功能以显示客户端。

Problems: I am not getting anything displayed on the browser, and unhelpfully I can only call the api 20 times per day, below is a sample of the JSON that is returned. 问题:我没有在浏览器上显示任何内容,无奈地,我每天只能调用api 20次,以下是返回的JSON的示例。

The JSON I am interested in that is returned is a string array Addresses, not the longitude and latitude. 返回的我感兴趣的JSON是一个字符串数组地址,而不是经度和纬度。 The other examples and questions I have seen that are similar to this have a key and value structure in their JSON for easy mapping. 我看到的其他与此类似的示例和问题在JSON中都具有键和值结构,以便于映射。

Question: How can I get the addresses returned from the AJAX call to display in the autocomplete drop down? 问题:如何获得AJAX调用返回的地址以显示在自动完成下拉列表中? I can spew them out into a div but autocomplete doesn't seem to acknowledge anything. 我可以将它们喷入div中,但自动完成功能似乎无法识别任何内容。 I have included the necessary resources: https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css and https://code.jquery.com/ui/1.10.4/jquery-ui.js 我已经包含了必要的资源: https : //code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.csshttps://code.jquery.com/ui/1.10.4 /jquery-ui.js

JSON from AJAX: - trimmed results because there are a lot: 来自AJAX的JSON: -修剪的结果,因为有很多:

{  
   "Latitude":51.991799,
   "Longitude":-1.078074,
   "Addresses":[  
      "1 Stable Close, , , , Finmere, Buckingham, Buckinghamshire",
      "10 Stable Close, , , , Finmere, Buckingham, Buckinghamshire",
      "12 Stable Close, , , , Finmere, Buckingham, Buckinghamshire",
      "14 Stable Close, , , , Finmere, Buckingham, Buckinghamshire",
      "16 Stable Close, , , , Finmere, Buckingham, Buckinghamshire"
   ]
}

JavaScript: JavaScript的:

var typingTimer; //timer identifier
var typingInterval = 2000; // 2 second delay
var pc = $('#pcode');
var divArray = $('#out4');

//on keyup, start the countdown
pc.keyup(function() {
  clearTimeout(typingTimer);
  if (pc.val) {
    typingTimer = setTimeout(function() {
      postcodeDelay();
    }, typingInterval);
  }
});

function postcodeDelay() {
  apiurl = 'https://api.getAddress.io/v2/uk/' + String(pc.val()) + '?api-key=key';
  // ajax call to the api too get JSON object of addresses that match the postcode entered
  $.ajax({
    type: "GET",
    url: apiurl,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: possiblePostcodes
  });
}

function possiblePostcodes(data) {

  pc.autocomplete({
    source: data.Addresses
  });
}

Html: HTML:

<div class="ui-widget">
  <label for="pcode">Enter a Postcode:</label>
  <input id="pcode" type="text" />
</div>
return {
      label: item.Addresses // picks the array from the JSON object.
}

One hint: in the browser developer tools, you can select your call to the service and save the response as a JSON file. 一个提示:在浏览器开发人员工具中,您可以选择对服务的调用,并将响应另存为JSON文件。 You could then set up a webserver and serve it from there, so you'd get around the 20 calls/day limit for developing. 然后,您可以设置一个Web服务器并从那里提供服务,这样您每天就可以限制20个呼叫的开发时间。

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

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