尝试从url获取JSON/JSONP数据,并将结果显示在html网页上


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

例如,当我输入url时,"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,而不是仅使用data

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

你想在哪里使用你的html id,作为回应,你将写数据,然后写数据,或者如果你使用结果,那么你将使用结果