如何使用getJSON将数据作为HTML从JSON对象输出 [英] How to output data as HTML from JSON object using getJSON

查看:148
本文介绍了如何使用getJSON将数据作为HTML从JSON对象输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我会尽量保持简单和简短。

Hello there I will try and keep this simple and short

我有一个每5秒运行一次的getJSON函数。现在,当我使用document.write函数显示数据时,它似乎想要更新。该页面卡在加载循环中。我怎样才能让数据在我的页面上显示?我的JSON很好,但我会告诉你。

I have a getJSON function that runs every 5 seconds. Now when I display the data using document.write function it dosent seem to want to update. The page is stuck in a loading loop. How can I get the data to display on my page? My JSON is fine but I will show you anyways.

  <script type="text/javascript">

 $.ajaxSetup ({  
    cache: false  
     });  

    setInterval(function(){ $.getJSON('names.json', function(data) {

    for(i in data)  {

        document.write(data[i] + "<br/>");
    }

  });



},5000);

 </script>

这是JSON对象

{
  "one": "",
  "two": "Beady little eyes",
  "three": "Little birds pitch by my doorstep"
}


推荐答案

code>文件撰写。一旦页面被加载,这将删除页面。使用(jQuery的)DOM方法来操作DOM。

Don't actually use document.write. Once the page is loaded, that will erase the page. Use (jQuery's) DOM methods to manipulate the DOM.

$.getJSON('names.json', function(data){
    for(var i in data){
        $('#myDiv').append(data[i]);
    }
});

这篇关于如何使用getJSON将数据作为HTML从JSON对象输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆