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:
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
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.
Discussion
View additional discussion.