Reload json.php in jquery every 1 minute?

Hi Experts,

I have a refresh.php file which will output the following json if called:

{"var1":"581","var2":"88","var3":"2477","var4":"102"}

Open in new window


Now I would like to read this refresh.php every 1 minute with jquery and basically get at the end the variables

var1 = 581
var2 = 88
etc.

to use in my other jquery functions. I tried some things but somehow never get it really work and I appreciate your help expert help in advance.
Oliver2000Asked:
Who is Participating?
 
Rainer JeschorCommented:
Hi,
the following should work:
var refreshTimer;
var total_rate = 0;
var total_user = 0;
var total_objects = 0;
var total_comment = 0;

$(document).ready(function () {
	GetRefresh();
	refreshTimer = window.setInterval(GetRefresh, 60000);
});

function GetRefresh() {
	$.get( "refresh.php", function( data ) {
		// This should work
		total_rate = data.total_rate;
		total_user = data.total_user;
		// The following should also work
		total_objects = data["total_objects"];
		total_comment = data["total_comment"];
		alert(Total rate:' + total_rate + ' - Total user: ' + total_user + ' - Total objects: ' +  total_objects + ' - Total comments: ' + total_comment);
		
	});
}

Open in new window

HTH
Rainer
0
 
Rainer JeschorCommented:
Hi,
this should do the trick:
var refreshTimer;

$(document).ready(function () {
	GetRefresh();
	refreshTimer = window.setInterval(GetRefresh, 60000);
});

function GetRefresh() {
	$.get( "refresh.php", function( data ) {
		// Do whatever you want with the response
		alert( "Data Loaded: " + data );
	});
}

Open in new window


HTH
Rainer
0
 
Oliver2000Author Commented:
Hallo Rainer,

This works so far but I dont know how to use a single value from my json.
I get in my alert now "Data Loaded: {"total_rate":"587","total_user":"89","total_object":"2520","total_comment":"102"}"
but how can I now use a single value like:

var total_rate = 587

in the example above?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Oliver2000Author Commented:
Hallo Rainer,

Not working for me. When I use any of this I get always UNDEFIND.

My code looks like this.

var refreshTimer;
var total_rate = 0;
var total_user = 0;
var total_objects = 0;
var total_comment = 0;

$(document).ready(function () {
	GetRefresh();
	refreshTimer = window.setInterval(GetRefresh, 60000);
});

function GetRefresh() {
	$.get( "refresh.php?type=ajax", function( data ) {
		// Do whatever you want with the response

		// This should work
		total_rate = data.total_rate;
		total_user = data.total_user;
		// The following should also work
		total_objects = data["total_objects"];
		total_comment = data["total_comment"];
		
                alert('Total rate:' + total_rate);                    // I get ERROR UNDEFIND!
		alert('Data Loaded: ' + data.total_rate);    // I get ERROR UNDEFIND!
                alert('Data Loaded: ' + data);                      // Works perfect and displays the entire Json result. 
	
	});
}

Open in new window


Aside of this all works fine, the refresh etc. all working. I just need the variables somehow as single variables.

Thanks
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
The data is coming back as a plain string.  To have jquery recognize it as a JSON string, please either add a 3rd parameter "json" to your $.get call, or use JSON.parse() in your callback function body.
0
 
Michel PlungjanIT ExpertCommented:
0
 
Oliver2000Author Commented:
Thanks to all experts here for the help. The solution from Rainer Jeschor worked already great except the information with the .getJSON was missing and kept my of track. Thank you all for your time and the great help. Since the bigger part of the job is dont by Rainer but the entire thing did not work out without the information from Brian I split the points accordingly. I hope this is correct like this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.