2017-10-29 58 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>"); 
     }); 
    }); 
}