Behseini
Behseini

Reputation: 6328

How to Load Data From Ajax Into An Existing Array

I need to store some retrieved data from Database into an array. I know we can present the data as this example:

$('#loader').click(function () {
    $.get(
        'results.php', {
            id: $(this).val()
        },
        function (data) {
            $('#result').html(data);
        }
    );
});

but how I can store the function(data){} into an array like var datalist = []

Thanks

Upvotes: 0

Views: 379

Answers (1)

brandonscript
brandonscript

Reputation: 73044

Assuming your incoming data is JSON, you can declare an object before calling your function, and then set data to datalist after the call completes:

var datalist = {};
$('#loader').click(function()
{
    $.get(
        'results.php', { 
            id : $(this).val() 
        },
        function(data) {
            datalist = JSON.parse(data);
        }
    );
});

Upvotes: 1

Related Questions