Skip to content

Instantly share code, notes, and snippets.

@WeeHorse
Created April 20, 2023 09:25
Show Gist options
  • Save WeeHorse/899335defde2dce9bc0fc5e4688962ca to your computer and use it in GitHub Desktop.
Save WeeHorse/899335defde2dce9bc0fc5e4688962ca to your computer and use it in GitHub Desktop.
send formdata as json
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="script.js" defer></script>
</head>
<body>
<form>
<input name="name" value="No name">
<input name="slogan" value ="No slogan">
<select name="pigs-in-a-blanket" multiple>
<option>No option</option>
<option value="pigs" selected>Pigs</option>
<option value="in">In</option>
<option value="a">A</option>
<option value="blanket">Blanket</option>
</select>
<input type="submit" value="submit">
</form>
</body>
</html>
document.querySelector('form').addEventListener("submit", submitForm)
const url = "https://1589dbb9-350b-40d2-9f8f-dd9df788b78f.mock.pstmn.io/login"
async function submitForm(e) {
e.preventDefault()
await fetch(url,{
method: 'post',
body: JSON.stringify(Object.fromEntries(new FormData(e.target)))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment