duanbipu1720
2015-07-01 09:08 阅读 247

尝试从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!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    doubo4336 doubo4336 2015-07-01 09:14

    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>
    
    点赞 评论 复制链接分享
  • dtwvr26066 dtwvr26066 2015-07-01 09:12

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

    点赞 评论 复制链接分享
  • douwei4370 douwei4370 2015-07-01 09:13
    $('#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

    点赞 评论 复制链接分享

相关推荐