Answers for "vanilla js post form data"

0

vanilla js post form data

document.addEventListener('submit', function (event) {

	event.preventDefault();

	fetch('https://jsonplaceholder.typicode.com/posts', {
		method: 'POST',
		body: JSON.stringify(Object.fromEntries(new FormData(event.target))),
		headers: {
			'Content-type': 'application/json; charset=UTF-8'
		}
	}).then(function (response) {
		if (response.ok) {
			return response.json();
		}
		return Promise.reject(response);
	}).then(function (data) {
		console.log(data);
	}).catch(function (error) {
		console.warn(error);
	});
});
Posted by: Guest on May-19-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language