从AJAX调用返回的结果创建列表

时间:2017-10-29 01:27:21

标签: jquery ajax api

我正在尝试将一些使用.get创建的代码放入使用ajax的内容中,但是我还没有能够将结果显示为页面上的列表。这个api有一份美国国家航空航天局的设施及其纬度和经度列表,这就是我在这里列出的代码。 这是使用get方法制作工作列表的原始代码:

$(document).ready(function(r){
  $.get("https://data.nasa.gov/resource/placeholder",function(r){
    r.forEach(function(el){
     $("ol").append("<li>"+el.facility+el+"</li>");
     console.log(el.location.human_address);

这是我尝试使用的新代码:

    var fullurl = "https://data.nasa.gov/resource/placeholder"
    function(r){
      $.ajax({
      url:fullurl,
      success:r.each(function(el)
      r.forEach(function(el){
       $("ol").append("<li>"+el.facility+el.location.latitude+el.location.longitude+"</li>");
       console.log(el.location.human_address);
       console.log(el);
})
)
}
})

以下是两者的html:     

<h1>N.A.S.A facilities</h1>

  <ol>

  </ol>
<input type="text" placeholder="what are you looking for?" id="t">
<button id="g"></button>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="main.js" type="text/javascript" charset="utf-8" async defer></script>

</body>

1 个答案:

答案 0 :(得分:0)

看来你需要这样的东西:

var fullurl = "https://data.nasa.gov/resource/placeholder";

function makeList() {
    return $.ajax({
        'url': fullurl
    }).then(function(results) {
        results.forEach(function(el) {
            $("ol").append("<li>" + el.facility + ' ' + el.location.latitude + ', ' + el.location.longitude + "</li>");
        });
    });
}
相关问题