2017-10-12 104 views
0

我試圖使用此api,以在我的網頁中顯示數據。 我想獲取數據並將其顯示在html段落中....我正在使用getJSON,但我無法使其工作..我正在使用AJAX發出請求。使用getjson獲取api數據javascript

樣品JSON數據

{ 
    "status":true, 
    "data":{ 
     "h1":0, 
     "h3":0, 
     "h6":0, 
     "h12":0, 
     "h24":0 
    } 
} 

的getJSON代碼

$.ajax({ 
type: "GET", 
url: "https://api.nanopool.org/v1/eth/avghashrate/1", 
dataType: "json", 
success: function(data) { 

    var json = $.parseJSON(data); 
    $('#results').html(json.data.h1); 
} 
}); 

HTML代碼的是

<div id="results"></div> 

回答

0

只是刪除$.parseJSON(),因爲data已經是一個object

$(function() { 
 
    $.ajax({ 
 
    type: "GET", 
 
    url: "https://api.nanopool.org/v1/eth/avghashrate/1", 
 
    dataType: "json", 
 
    success: function(data) { 
 
     console.log(typeof data); // -- Object 
 
     var json = data; 
 
     $('#results').html(json.data.h1); 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="results"></div>