I'm populating an array nodevalues with objects. It looks like this:
nodevalues.push({id: this.id, left: left, right: right});
This line is inside a $.each() iterator which iterates over some li nodes and also calculates the left and right values.
So I have an array with several objects who all look the same:screen dump chrome console
Is it possible to serialize this array to an url string for database storage using jQuery.post()?
Calling $(nodevalues).serialize() returns nothing and $.param(nodevalues) returns undefined=undefined&undefined=undefined&undefined=undefined&undefined=undefined&undefined=undefined&undefined=undefined&undefined=undefined&undefined=undefined&undefined=undefined&undefined=undefined...
3 Answers 3
jQuery.param() might do what you want
var dataString = jQuery.param(nodevalues);
It seems that param only works on objects, not arrays.
If you wrap your array in an object it works fine:
var nodevalues = [];
nodevalues.push({id: "node1", left: 1, right: 20});
nodevalues.push({id: "node2", left: 2, right: 10});
nodevalues.push({id: "node3", left: 3, right: 30});
nodevalues.push({id: "node4", left: 4, right: 40});
var data = { data: nodevalues };
alert($.param(data));
1 Comment
You could use the stringify function from the JSON library.
Comments
you can supply the data as second argument to jquery.post()
jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] );
jquery will serialize it for you.
1 Comment
$.post('ajax/changenode.php', nodevalues); Results in: Form data: undefined:undefined undefined:undefined undefined:undefined undefined:undefined undefined:undefined undefined:undefined undefined:undefined undefined:undefined undefined:undefined undefined:undefined Doesn't do the job!
$(this).attr('id'). "this" is .each() is the DOM element ->this.id