const requestURL = 'https://your-url.com';
function sendRequest(method, url, body) {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
return fetch(url,
{
method,
body,
headers,
}).then((response) => {
if (response.ok) {
return response.text();
}
return response.text().then((error) => {
const err = new Error();
err.data = error;
throw err;
});
});
}
const form = document.getElementById('form');
form.addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(form);
const values = Object.fromEntries(formData.entries());
sendRequest('POST', requestURL, JSON.stringify(values))
.then((data) => console.log(data))
.catch((err) => console.log(err));
// sendRequest('GET', requestURL, null)
// .then((data) => console.log(data))
// .catch((err) => console.log(err));
});