Rauno Rauno - 3 months ago 19
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) {
console.log(jQuery.parseJSON(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

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

https://api.jquery.com/jquery.get/

Furthermore you can always parse the JSON object in response.

Example:

$.get( "test.php", function( data ) {
 $( "body" )
 .append( "Name: " + data.name ) // John
 .append( "Time: " + data.time ); //  2pm
}, "json" );