Jump to content

Json Data issue


ibrahimjan
 Share

Recommended Posts

Hi I have a json Data coming from an ASP script, this works all fine, please have a look at the data at:http://jancodesign.c...YID=-1422732274now I have created my JS to read this data please have a look at: http://jancodesign.com/js/JsonRead.js      The problem is that I get the "DesignID" to show but not the "TxText" instead of the data to show I get "undefined"Any Ideas as why this is happening??Thank you

Edited by Ibrahim Home Jan
Link to comment
Share on other sites

why are you using a +i as your index for accessing the data array? Why not just data? Also, you shouldn't really just loop through an array arbitrarily, with a predefined end conditional, (1000). Usually you use an array's length to determine the stopping point. And you should probably close your <p> tags. anyway, the reason you are getting undefined for TxText is because data.Text is also an array. you would need to loop through that in order to output it. i.e.

for(var i = 0, l = data.length; i < l; i++){  var d = data[i];    $('.inner').append('<p>' + d.DesignID + '</p>');   for(var j = 0, k = d.Text.length; j < k; j++){	$('.inner').append('<p>' + d.Text[j].txText + '</p>');  }}  

Edited by thescientist
Link to comment
Share on other sites

Hi I moved things around a bit, and came up with the following. Is there any way I can add a refresh function, to get new json data every 30 sec?? without refreshing the page. Thanks.

$.ajax({  type: "GET",data: "{}",contentType: "text/plain",dataType: "json",url: "/jsondata.asp?LAYID=-1707637504", cache: false,success: function(data, status) {					   for(var i = 0, l = data.length; i < l; i++){  var d = data[i];   $('.inner').append('<p><h1>' + d.DesignID + '</h1></p>');   //This is the Text Array///for(var j = 0, k = d.Text.length; j < k; j++){  $('.inner').append('<p><h2>TEXT'+[j+1]+'</h2></p>');  $('.inner').append('<p>' + d.Text[j].TxText + '</p><hr>'); }}}});

Edited by Ibrahim Home Jan
Link to comment
Share on other sites

I added setinterval around the ajax, but I am getting the data to reload new set every 5sec, rather than refreshing the current data? please have a look.

setInterval(function(){			  	   $.ajax({		 dataType: "json",				    url: "/jsondata.asp?LAYID=-1707637504",				    cache: false,				    success: function(data, status) {						 for(var i = 0, l = data.length; i < l; i++){var d = data[i];   $('.inner').append('<p><h1>' + d.DesignID + '</h1></p>'); //This is the Text Array/// for(var j = 0, k = d.Text.length; j < k; j++){  $('.inner').append('<p><h2>TEXT'+[j+1]+'</h2></p>');	    $('.inner').append('<p>' + d.Text[j].TxText + '</p>');}}},});}, 5000);

Link to comment
Share on other sites

what do you expect the difference to be between reloading the data and refreshing the data? If you need the data to be updated somehow before it reaches the client, then that's something you need to do on the servers end where the data is being generated and is being requested from.

Link to comment
Share on other sites

If new data is created or updated on the server on a regular intervals, the js needs to check every 5sec or so to reload the new data, the ASP file is producing the new data. to prove that at the moment the only way to see the new data take place is by refreshing the full page. Can I not just refresh the Div's or that wont work?? thanks

Edited by Ibrahim Home Jan
Link to comment
Share on other sites

If you say you only see the updates on an actual page refresh, then there might be another issue. You would need to debug your request and response to determine what is different in each case. Or better yet, show more code. Log your request/responses in the console to see what they are. Try using Chrome and inspecting the request/response in the Network tab. Try making requests directly to your ASP script and determine if there are any differences in the actual requests that would be giving you different responses.

Edited by thescientist
Link to comment
Share on other sites

If the data is being cached then you can add a random number to the querystring to disable caching.
In theory, this should be doing that with cache: false
  $.ajax({	 dataType: "json",	 url: "/jsondata.asp?LAYID=-1707637504",	 cache: false,	 success: function(data, status) {}  })

http://api.jquery.com/jQuery.ajax/
If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.
Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...