Skip to content Skip to sidebar Skip to footer

Function Result Array Inside $.ajax Is Converted To String

I need to pass array of ids with $.ajax data variable. The array is a result of a function. If i declare this function outside $.ajax it sends array correctly. But if i put same fu

Solution 1:

Well make sure that you invoke this anonymous function in order to assign the proper result (array of strings) to the ordering parameter:

data: {
    ordering: (function () { // Do not pass hidden clonesvar items = [];
        $('#fp_parameters_list').children().each(function() {
            if ($(this).is(':visible')) {
                 items.push($(this).attr('data-parameter-id'));
             }
         });
         return items;
    })(); // <!-- Here call the anonymous function to get its result
}

Solution 2:

Just use $.map to build the array directly instead

$.ajax({
  url: '/echo/json/',
  type: 'post',
  dataType: 'json',
  data: {
    ordering: $.map($('#fp_parameters_list').children(':visible'), function(el) {
                 return $(el).data('parameter-id');
              })
  }
});

Post a Comment for "Function Result Array Inside $.ajax Is Converted To String"