Message from JavaScript discussions

November 2020

— Are you reading in my mind? My code is literally async loops

— 

Hello, I'm stuck with something what is the best way to make an AJAX request with a form to a Rest API? This is wat I have so far now:

const myForm = document.getElementById('loginForm');

myForm.addEventListener('submit', (e) => {
e.preventDefault();

const username = document.getElementById('username').value;
const password = document.getElementById('password').value;

fetch('http://localhost:4000/login', {
method: 'POST',
body: JSON.stringify({
username: username,
password: password
}),
headers: {
'Content-Type': 'application/json'
}
})
.then(res => {
res.json().then((data) => {
if (res.status === 400) {
document.getElementById('username-group').classList.add('has-error');
document.getElementById('password-group').classList.add('has-error');
}
})
})
.catch(error => console.log(error));
});

— Is it safe to get the values from the form by this way or not?

— Forms aren't necessary for xhr/fetch requests

— Also for logins?

— Is that safe?

— Form request is made by the browser, includes navigation, navigation aka transition is usual for auth process but also not necessary

Message permanent page

— So you are saying I can skip the form part? And just get the values from the form fields and send that to the API?

Message permanent page

— Nothing is safe🤤 https isn't safe, let's encrypt cert times out, site crashes

— You may not use forms at all, only inputs and their values, ye

— So thats a better solution you would say?

— Forms may be useful too, for structuring markup or getting grouped data