javascript - Sending an array per ajax request -


i want send ajax request array(which can length) 1 parameter, :

mandatoryfields = []; (i = 1; <= post_type_max_elements; i++) {     ...     var mandatoryitem = []     mandatoryitem['optionname'] = optionname;     mandatoryitem['optionvalue'] = optionvalue; }   var data = {     action: 'update_mandatory_fields',     post_type: select_post_type,     mandatoryfields: mandatoryfields }; jquery.ajax({     type: 'post',     datatype: "json",     data: data,     ... 

seems, not work, on server parameter mandatoryfields not defined. best way?

thanks!

you can use json.stringify() convert array string.

the json.stringify() method converts javascript value json string, optionally replacing values if replacer function specified, or optionally including specified properties if replacer array specified.

in example:

$.ajax({   type: "post",   url: url,   data: {yourdata: json.stringify(data)},   success: function(response){       //callback function - if request succeeded   } }); 

note can use $.post() instead of $.ajax().

to reconstruct array php, can use json_decode($_post['yourdata']);


Comments