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

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

问题描述:

When I enter the url for instance, "www.example.com" I get the return:

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

I am trying to get all this data and displaying it on my website that is HTML. I tried researching and even tried the following code,

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

But it just displays nothing. Please help!

例如,当我输入网址时,“www.example.com” code>我得到了 返回: p>

  {
 shh:{
 id:3730672,
 name:“Shh”,
 profileIconId:23,
 summonerLevel:2,\  n revisionDate:1421646418000 
} 
} 
  code>  pre> 
 
 

我正在尝试获取所有这些数据并将其显示在我的HTML网站上。 我试过研究甚至尝试过以下代码, p>

  $。getJSON('www.example.com',function(data){
 alert(data); \  n}); 
  code>  pre> 
 
 

但它只显示任何内容。 请帮忙! p> div>

if your data callback is not empty you can try this :

{ 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>

</div>

Have you tried putting the breakpoint there and see what data contains? Also you should use data.d in alert instead of just data

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

where you want to put your html id use and in response you will write data then write data or if you use result then you will use result