Hello there I will try and keep this simple and short
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>
This is the JSON object
{
"one": "",
"two": "Beady little eyes",
"three": "Little birds pitch by my doorstep"
}
I would recommend you use jQuery,
I used this to create a form from my json item, I hope this helps...
And my JSON would look like this
This code adds the form in to my div tag with the id mainContainer I know its alot of code, but i hope it helps !
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.You want to render dom which will contain the data, then when you get the data update the dom.
As an exceedingly simple example, on your page have a container
and then in your ajax success handler
This uses jquery to grab the dom element with id "one", and update its text.