zoukankan      html  css  js  c++  java
  • [HTML 5] Transforming FormData for the Server (URLSearchParams)

    const app = document.getElementById('app');
    app.innerHTML = `
      <h1>JavaScript DOM</h1>
      <form name="order">
        <label>
          Your name
          <input type="text" name="fullname">
        </label>
        <label>
          Which pizza would you like?
          <select name="pizza">
            <option value="pepperoni">Pepperoni</option>
            <option value="meaty">Meaty</option>
            <option value="cheesey">Cheesey</option>
          </select>
        </label>
        <div>
          What size?
          <label>
            Small
            <input type="radio" name="size" value="small" checked>
          </label>
          <label>
            Medium
            <input type="radio" name="size" value="medium">
          </label>
          <label>
            Large
            <input type="radio" name="size" value="large">
          </label>
        </div>
        <label>
          Quantity
          <input type="number" name="quantity" value="1">
        </label>
        <button type="submit">
          Submit
        </button>
      </form>
    `;
    
    const form = document.forms.order;
    
    function handleSubmit(event) {
      event.preventDefault();
      const formData = new FormData(event.target);
    
      // query string
      // Content-Type = application/x-www-form-urlencoded
      // fullname=Todd%20Motto&pizza=pepperoni&size=large&quantity=2
      // const data = [...formData.entries()];
      // const asString = data
      //   .map(x => `${encodeURIComponent(x[0])}=${encodeURIComponent(x[1])}`)
      //   .join('&');
    //. or using URLSearchParams
    const asString = new URLSearchParams(formData).toString(); console.log(asString); // json const asJSON = JSON.stringify(Object.fromEntries(formData)); console.log(asJSON); } form.addEventListener('submit', handleSubmit);
  • 相关阅读:
    cookie与session的区别
    基于TCP协议的网络编程
    springboot第一篇:springboot基础
    java中的正则表达式
    NIO
    io基础(字节流、字符流、转换流、缓冲字符流)
    基于UDP协议的网络编程
    es6.3学习笔记
    线程同步和线程通信
    java字符串各种编码
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13159361.html
Copyright © 2011-2022 走看看