2017-10-29 81 views
0

我正在尝试一些代码,这些代码是通过.get将某个代码变成了利用ajax的东西,但是我没有能够得到结果以显示为列表在页面上。这api有一个美国国家航空航天局的设施和他们的纬度和经度的列表,这是我想列出的代码。 这是使用GET方法进行的设备的清单,其中工程原代码:从AJAX调用返回的结果创建列表

$(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> 
+0

AJAX并没有任何关系做创建列表。你展示的一部分,如果它正确创建一个列表,与显示无关。你也需要展示那部分内容。 [mcve] – Rob

+0

我在使用get方法时添加了旧代码。也许这会更多地阐明事情。 – sacora

+0

您显示的内容与显示没有任何关系,即HTML/CSS。 – Rob

回答

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>"); 
     }); 
    }); 
}