user3764429
user3764429

Reputation: 402

How to return a serialized array in JavaScript/jQuery?

I have the following problem:

I have a form and I want to catch the input data. When I want to obtain string, which stores the input from this form, I simply do:

JSON.stringify($('form').serializeArray())

But instead of obtaining string, I would like to have an array with my data (preferably, a serialized array).

How can I solve this problem?

Thanks in advance.

Upvotes: 0

Views: 202

Answers (2)

Barath
Barath

Reputation: 102

Don't stringify the array.

JSON's .serializeArray() method returns an array of the all the input values in the form

Upvotes: 0

neel shah
neel shah

Reputation: 2281

.serializeArray() creates an array of objects,JSONStringify() converts it to string

You can use

$( "form" ).submit(function( event ) {
console.log( $( this ).serializeArray() );
event.preventDefault();
});

Upvotes: 1

Related Questions