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 :: dangerouslySetInnerHTML 
Javascript :: mongodb sort query 
Javascript :: react typed js 
Javascript :: js reduce break 
Javascript :: disable key enter react-hook-form 
Javascript :: javascript array to table 
Javascript :: regex phone number 
Javascript :: class element in javascript 
Javascript :: express request body undefined 
Javascript :: js named parameters 
Javascript :: chartjs Uncaught ReferenceError: Utils is not defined 
Javascript :: window.print a div 
Javascript :: javascript parseint string with comma 
Javascript :: jquery boilerplate 
Javascript :: MongoNotConnectedError: Client must be connected before running operations 
Javascript :: javascript string interpolation 
Javascript :: multiple click events in react 
Javascript :: js random string from array 
Javascript :: check if variable is jquery object 
Javascript :: dynamic import javascript 
Javascript :: javascript random 4 digit number 
Javascript :: epsilon javascript 
Javascript :: JSON.parse() error handling 
Javascript :: nodejs: read and write file: use fs and promise 
Javascript :: default value input date js 
Javascript :: get unique array javascript 
Javascript :: js get last array element 
Javascript :: install ckeditor 5 for react js 
Javascript :: return symmetric difference of the array javascript 
Javascript :: javascript count time 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =