javascript - Serialize and de-serialize array (without jquery?) -
javascript - Serialize and de-serialize array (without jquery?) -
i have method sends ajax request. when reply server received need serialize , later de-serialize
$.ajax({ //..... done(function(data) { //1 need serialize info (which array) }); function myfunction() { //2 need de-serialize info has been serialized } i know utilize jquery#serializearray() if had form serialize:
$( "form" ).submit(function( event ) { console.log( $( ).serializearray() ); event.preventdefault(); }); but don't have form , info server (i guess) has nil serializearray function of jquery. how can it? what's 1 of best ways?
preferably not utilize third-party libraries except jquery or not utilize jquery @ all.
the mutual way serialize js-objects json via json.stringify().
the other way around via json.parse().
o={"firstname":"john","lastname":"doe"}; console.log(json.stringify(o)); console.log(json.parse(json.stringify(o))); see mdn stringify , parse
here fiddle.
.serializearray() jquery neat helper function serialize form-data. builds objects ground up. here source that. if want submit info json,
$.ajax({ type: "post", url: url, data: json.stringify(data), success: success, datatype: datatype }); free after jquery.post().
javascript jquery ajax serialization
Comments
Post a Comment