Source Code:
(back to article)
Submit
Result:
Report an issue
<form id="myForm" onsubmit="onSubmit(event)"> <input type="text" name="title" value="A title"> <input type="text" name="body" value="A body"> <input type="submit" value="Submit Post"> </form> <div>post id: <span id="response"></span></div> <script> function onSubmit(event) { event.preventDefault(); const form = document.getElementById('myForm'); const formData = new FormData(form); const responseSpan = document.getElementById('response'); fetch('https://jsonplaceholder.typicode.com/posts', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { responseSpan.innerHTML = data.id; }) .catch(error => console.error('Error:', error)); } </script>