Skip to content

Latest commit

History

History
29 lines (24 loc) 路 622 Bytes

2022-01-14.md

File metadata and controls

29 lines (24 loc) 路 622 Bytes
publish_date tags
2022-01-14
javascript
webdev
TIL
  • Neat trick to handle form data using FormData. It converts form data into a js object.
  const handleSubmit = async (event: any) => {
    event.preventDefault();

    const form = new FormData(event.target);

    const formData = Object.fromEntries(form.entries());

    await fetch('/api/contact', {
      body: JSON.stringify(formData),
      headers: {
        'Content-Type': 'application/json',
      },
      method: 'POST',
    });

    // const result = await res.json();
  };