jquery - How do I add a variable to the end of an object's key? -
i'm trying pass array filled objects:
var steps = [ { bla: blu, bla: blu, bla: blu }, { // etc.. 3 more times same format } ];
to codeigniter controller using jquery's $.ajax() function.
i tried way:
$.ajax({ url: base_url + 'index.php/worldmap/ajax/start_travelling', type: 'post', data: { steps: steps }, success: function(response){ console.log(response); } });
but got typeerror , found out data object passed controller must in key => value format. read take single object.
so figured if passed number of steps , each individuel step i'll put them in array again in codeigniter controller.
so ended with:
var datatosend = { num_steps: steps.length }; var = 1; $.each( steps, function( index, value ) ){ datatosend.step+i = value; // want key step_1, step_2 etc.. += 1; }; $.ajax({ url: base_url + 'index.php/worldmap/ajax/start_travelling', type: 'post', data: datatosend, success: function(response){ console.log(response); } });
but can guess .step + 1 not seem work. right syntax i'm trying do? or if knows better way of passing multi-level array ci controller please share :d
your json object correctly formed, should able read data php, provide more info showing php code.
the thing missing stringify data:
var steps = [ { bla: blu, bla: blu, bla: blu }, { // etc.. 3 more times same format } ]; var datatosend = { steps: steps }; $.ajax({ url: base_url + 'index.php/worldmap/ajax/start_travelling', type: 'post', data: json.stringify(datatosend), success: function(response){ console.log(response); } });
Comments
Post a Comment