Created
April 20, 2023 09:25
-
-
Save WeeHorse/899335defde2dce9bc0fc5e4688962ca to your computer and use it in GitHub Desktop.
send formdata as json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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