Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Send JSON from HTML form with NODEJS backend

Send JSON from HTML form with NODEJS backend

Problem

I haven't done front end Html since I was 10 and that was drag and drop frontpage stuff. with static pages. As a result I'm really rusty.

What I need to do is put together a web client for a rest API that I wrote in NodeJS. My question is how, do you send a request from a form (say a log in form) to the server where the body of the POST request is a JSON of the email/password?

HTML form:

Log in
Problem courtesy of: Alex B

Solution

I suggest a lot of reading. To get you started with a very basic example, though, you will find a page with a sample form below that does what you need. You just need to replace the string your URL here with the actual URL you expect will be doing the handling.

The serializeObject() function was taken from here: Convert form data to JavaScript object with jQuery


    
        
Username:
Password:

The problem with your form is that input elements don't have name attributes. The name attribute is essential in many ways, so I would fix your html by setting each element's name attribute to the same value as its id attribute. The serializeObject function relies on form elements having names.

Solution courtesy of: Ioannis Karadimas

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Send JSON from HTML form with NODEJS backend

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×