0

I use Angular and Node.js + Express 4 + socket.io.

I want send form to the server throw socket.io. I created a service in my Angular app which returns serialized form data. This service works the same way as jQuery.serialize().

Then I send serialized data to the server using socket.io. How can I parse this data to the JSON object? I have express body-parse but I don't know how to use it not like a express middleware.

0

1 Answer 1

4

According to the jQuery docs, the jQuery.serialize() will create a query string with the form elements in the following form (example):

single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1

To parse a query string into an object, you can use the querystring Node.js module:

var qs = require('querystring')
qs.parse('single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1')
// Prints:
{ single: 'Single',
  multiple: [ 'Multiple', 'Multiple3' ],
  check: 'check2',
  radio: 'radio1' }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.