duanbipu1720
2015-07-01 09:08
浏览 487
已采纳

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

图片转代码服务由CSDN问答提供 功能建议

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

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

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

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

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题