{///
headers: {
"Content-Type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded',
}
///
}
// layout.pug
html
head
block title
title Layout Default Title
body
block content
// index.pug
extends layout.pug
block title
title Index page
block content
p Lorem ipsum dolor sit amet.
document.querySelector('form')?.addEventListener('submit', (event) => {
const form = new FormData(event.currentTarget);
let isValid = true;
if (form.get('name').length <= 2) {
isValid = false;
}
if (!isValid) {
// Отмена отправки формы
event.preventDefault();
}
alert(`Is Valid: ${isValid}`);
});
function getPosts(callback) {
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://jsonplaceholder.typicode.com/posts");
xhr.addEventListener("load", () => {
const response = JSON.parse(xhr.responseText);
console.log(response);
callback(response);
})
xhr.addEventListener("error", () => {
console.log("error!");
})
xhr.send();
}