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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.