簡體   English   中英

嘗試從url獲取JSON / JSONP數據並將結果顯示到html網頁上

[英]Trying to Fetching JSON/JSONP data from url and displaying the result onto a html webpage

當我輸入網址例如"www.example.com"我得到了返回:

{
    shh: {
        id: 3730672,
        name: "Shh",
        profileIconId: 23,
        summonerLevel: 2,
        revisionDate: 1421646418000
    }
}

我正在嘗試獲取所有這些數據並將其顯示在我的HTML網站上。 我嘗試研究甚至嘗試以下代碼,

$.getJSON('www.example.com', function(data){
    alert(data);
});

但是它什么也沒顯示。 請幫忙!

如果您的數據回調不為空,則可以嘗試以下操作:

 { shh: { id: 3730672, name: "Shh", profileIconId: 23, summonerLevel: 2, revisionDate: 1421646418000 } } $.getJSON('www.example.com', function(data){ if(data){ var ssh = data.ssh; var ssh_id = ssh.id; var ssh_name = ssh.name; $('.name').html(ssh_name); $('.id').html(ssh_id); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <span class="name"></span> with id:<span class="id"></span> 

您是否嘗試過將斷點放置在那里並查看包含哪些數據? 另外,您應該在警報中使用data.d而不是數據

$('#id').html(data.shh.id);

您想在其中使用html id的位置,作為響應,您將寫入數據,然后寫入數據,或者如果您使用result,那么您將使用result

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM