Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Simple enough. Suppose we have something written in localStorage by the key test :
To send this data to the server, simply run:
localStorage.setItem('test', 'Что-то');
sendInfo('test', 'https://ваш_сайт/handler.php');
function sendInfo(key, url) {
// Создаем форму в конструкторе:
var formData = new FormData();
// Добавляем поле с данными в форму:
formData.append(key, localStorage.getItem(key));
// Создаем запрос:
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
// Обработка ответа:
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
alert('Запрос выполнен успешно, ответ сервера: ' + xhr.responseText);
}
else {
alert('При выполнении запроса произошла неизвестная ошибка!');
}
}
}
// Отправка:
xhr.send(formData);
}
Use Ajax requests to the server
https://learn.javascript.ru/fetch
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question