Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

html form post json example

submitButton.addEventListener("submit", (event) => {
  const formData = new FormData(event.target);
  // object used to collect form data
  const objToSend = new Object();
  formData.forEach((value, key) => {
    if (!Reflect.has(objToSend, key)) {
      // property key encountered for first time
      Reflect.set(objToSend, key, value);
    } else {
      if (!Array.isArray(objToSend[key])) {
        // convert value into array
        objToSend[key] = [objToSend[key]];
      }
      // add new value to array
      objToSend[key].push(value);
    }
  });
  // Stringify object holding form data
  const objAsJSON = JSON.stringify(objToSend);
  console.log(objAsJSON);
});
Comment

javascript form post json data

var formData = JSON.stringify($("#myForm").serializeArray());
Comment

PREVIOUS NEXT
Code Example
Javascript :: set timeout JS for loop 
Javascript :: hwo to make ana array of prime numbers in javascript 
Javascript :: javascript loop backwards through array 
Javascript :: on() jquery 
Javascript :: javascript toisostring without milliseconds 
Javascript :: discord client.send_message js 
Javascript :: javascript get item in array by id 
Javascript :: sticky footer react 
Javascript :: add new items in a select input using js 
Javascript :: how to add field to object in js 
Javascript :: generate component in angular 
Javascript :: react antd form disable submit button 
Javascript :: react native header 
Javascript :: js array comprehension 
Javascript :: javascript write to text file 
Javascript :: how to find for lable in jquery 
Javascript :: passing html vlaues to Javascript function 
Javascript :: node js return ID in postgres insert 
Javascript :: string json to class c# 
Javascript :: error An unexpected error occurred: "https://registry.yarnpkg.com/@material-ui/icons/-/icons-4.11.2.tgz: ESOCKETTIMEDOUT". 
Javascript :: pretty alerts js 
Javascript :: javascript async function 
Javascript :: fromcharcode in javascript 
Javascript :: next router 
Javascript :: move canvas element to mouse 
Javascript :: react native counter 
Javascript :: how remove the spaces from the string, then return the resultant string 
Javascript :: react native webview not working 
Javascript :: javascript competitive programming 
Javascript :: scroll to top 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =