Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch jquery ajax call into array

Tags:

jquery

ajax

I would like to know if it is possible to return the ajax call into a javascript array, rather than a DOM object. The ajax call is returning something like n-tuples of value1-value2, and I would like to iterate over that.

Thanks.

$(function () {
    $("#categoria").change(function() {
        var id = $(this).val();
        alert(id);
        vars = load("getcategoria.php", "q="+id); // can I do something like this? how?
    });
});
like image 544
nunos Avatar asked Dec 02 '25 06:12

nunos


1 Answers

Yes you can do this, but .load() is the wrong method for the job. .load() is used to load HTML into a specific DOM element.

I suggest you have your getcategoria.php return a JSON object (use PHP's json_encode), and then use $.getJSON() to get it, parse it, and use it how you wish.

$.getJSON("getcategoria.php", "q="+id, function(data){
  // data is your JSON object, use it how you wish
});
like image 110
Rocket Hazmat Avatar answered Dec 04 '25 21:12

Rocket Hazmat