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);
  • 相关阅读:
    Kali Linux软件更新日报20190622
    Maltego更新到4.2.4.12374
    Kali Linux又增加一个顶级域名kali.download
    Nessus提示API Disabled错误
    数据包分析中Drop和iDrop的区别
    快速识别Hash加密方式hashid
    识别哈希算法类型hash-identifier
    vue实现前端导出excel表格
    vue自动化单元测试
    Mock制作假数据
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13159361.html
Copyright © 2011-2022 走看看