Fetch jquery ajax call to array

I would like to know if it is possible to return an ajax call to a javascript array and not a DOM object. Ajax call returns something like n-tuples value1-value2, and I would like to repeat that.

Thank.

$(function () {
    $("#categoria").change(function() {
        var id = $(this).val();
        alert(id);
        vars = load("getcategoria.php", "q="+id); // can I do something like this? how?
    });
});
+3
source share
3 answers

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

I suggest you return getcategoria.phpthe JSON object (use PHP json_encode) and then use $.getJSON()to get it, parse it and use it however you want.

$.getJSON("getcategoria.php", "q="+id, function(data){
  // data is your JSON object, use it how you wish
});
+2
source

jQuery getJSON() JSON, .

, JSON, . , .

  $.getJSON('ajax/test.json', function(data) {
  var items = [];

 $.each(data, function(key, val) {
   items.push('<li id="' + key + '">' + val + '</li>');
 });

  $('<ul/>', {
   'class': 'my-new-list',
   html: items.join('')
  }).appendTo('body');
});
0

And do not forget to install header('content-type: application/json')getcategoria.php in your file

0
source

All Articles