0

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!

Smit Patel
  • 2,992
  • 1
  • 26
  • 44
Rauno
  • 616
  • 8
  • 22
  • Do visits [How to return the response from an AJAX call](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call), I think its duplicate, However don't want to use dupehammer. – Satpal Aug 17 '16 at 06:22

2 Answers2

2

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" );
Kapil Yadav
  • 650
  • 1
  • 5
  • 29
  • This is not quite what I'm looking for (or maybe I just don't realise it is). The problem is that I get an array from database and then depending on what's happening on the page (you display a member from the array and if something changes again, you display another one). The way above (at least that's how it seems to me) gets the data, displays part of it it but once you want it to display another part, it asks for the data again, correct? That would mean a single user makes like 20-n queries (same ones) to the server which I don't want to do. Thanks for the answer though! – Rauno Aug 18 '16 at 06:14
0

Alright, I figured out the answer now thanks to 'kapil yadav'.

$.get('ajax.php', getData('send'), function (html) {
    //$('.slider' + value).html(html);
    $( "body" ).data( "sliderData", html);
}, "json");

console.log($( "body" ).data());

This is what I used to get what I needed in case anyone else stumbles here.

Rauno
  • 616
  • 8
  • 22