Passing an array to json.stringify

I am trying to pass an array to json.stringify, but the return value is returned empty.

JSON.stringify({ json: data }) // returns `{"json":[]}` 

And here is the data content:

 data[from] = " bfleming@test.com " data[to] = " test@test.com " data[message] = "testmessage" 

jquery:

 function SubmitUserInformation($group) { var data = {}; data = ArrayPush($group); $.ajax({ type: "POST", url: "http://www.mlaglobal.com/components/handlers/FormRequestHandler.aspx/EmailFormRequestHandler", data: JSON.stringify({ json: data }), dataType: 'json', contentType: "application/json; charset=utf-8", cache: false, success: function (msg) { if (msg) { $('emailForm-content').hide(); $('emailForm-thankyou').show(); } }, error: function (msg) { form.data("validator").invalidate(msg); } }); } function ArrayPush($group) { var arr = new Array(); $group.find('input[type=text],textarea').each(function () { arr[$(this).attr('id')] = $(this).val(); }); return arr; } 
+7
source share
2 answers
 data = ArrayPush($group); 

Overwrites data as an array, so all of your expando properties are not built.

Inside your ArrayPush method, change

 var arr = new Array(); 

to

 var obj = { }; 
+5
source

arr must be declared as an object inside the ArrayPush method, because you are not using it as an array. Also inside the function you can just use this.id and this.value , you do not need to create a jQuery object. try it

 function ArrayPush($group) { var arr = {}; $group.find('input[type=text],textarea').each(function () { arr[this.id] = this.value; }); return arr; } 
+2
source

All Articles