vanilla js post form data

Solutions on MaxInterview for vanilla js post form data by the best coders in the world

showing results for - "vanilla js post form data"
Luis
15 Oct 2018
1document.addEventListener('submit', function (event) {
2
3	event.preventDefault();
4
5	fetch('https://jsonplaceholder.typicode.com/posts', {
6		method: 'POST',
7		body: JSON.stringify(Object.fromEntries(new FormData(event.target))),
8		headers: {
9			'Content-type': 'application/json; charset=UTF-8'
10		}
11	}).then(function (response) {
12		if (response.ok) {
13			return response.json();
14		}
15		return Promise.reject(response);
16	}).then(function (data) {
17		console.log(data);
18	}).catch(function (error) {
19		console.warn(error);
20	});
21});
22