Rauno Rauno - 1 year ago 84
Ajax Question

Getting data from database to jquery variable

I'll make this short.

Basically the problem I'm facing right now is that I have a task where data from database is needed to update some texts in browser (I have some sliders that have text under them and depending on the position of the slider the text needs to update according to what's in database). Now I can get the texts to jquery with ajax but I can't seem to get it out to a variable.

This is how the code in jquery looks like:

$.get('ajax.php', getData('send'), function(html) {

Before you start merging this question with others, I did look through a ton of them but all I didn't quite see what I was looking for because I actually have to do a lot of different things with that data, not just display it. If there's anyone kind enough to explain how I can get that 'html' to a variable I would be very grateful.

So yeah, end goal is to have something like
var data = jQuery.parseJSON(html);
and that it behaved like a normal variable with some data in it from there on.

Thanks in advance and have a nice day!

Answer Source

Have a look to Official documentation of AJAX calls in Jquery.


Furthermore you can always parse the JSON object in response.


$.get( "test.php", function( data ) {
 $( "body" )
 .append( "Name: " + data.name ) // John
 .append( "Time: " + data.time ); //  2pm
}, "json" );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download