How can I pass an associative array to a server using jQuery? There is the following array:

arr = []; arr['param1'] = 'text1'; arr['param2'] = 'text2'; arr['param3'] = 'text3'; 

The normal transmission method does not work:

 jQuery.ajax({ url: path, async: false, type: 'POST', data: {'array': arr}, dataType: 'json', success: function(data) {...} }); 

    1 answer 1

    An associative array in JS is an object. Actually, an array with numeric keys is also an object, but initialization and generation is somewhat different.

     var obj = {}; obj.param1 = 'text1'; obj.param2 = 'text2'; obj.param3 = 'text3'; jQuery.ajax({ url: path, async: false, type: 'POST', data: {'array': obj}, dataType: 'json', success: function(data) {} }); 

    PS By the way, like this - data: obj, - also possible