Toskan
Toskan

Reputation: 14931

how to properly encode a form into json using jquery

I did this

let myform = $('#myapp-product-submit-form');
let jsonFormData = JSON.stringify(myform.serializeArray());
$.post( '/coaltechtest' , jsonFormData , function(datas){

problem:

the result contains a hundred name value pairs, e.g. written out

{
'name': 'product_name',
'value': 'some cool product name'
...}

but i want

{
'product_name': 'some cool product name'
}

Upvotes: 0

Views: 24

Answers (1)

Shadowfool
Shadowfool

Reputation: 1036

Map the values out.

let newArray = oldArray.map( item => { return { item.name: item.value}})

In your example oldArray is myform.serializeArray()

Upvotes: 1

Related Questions