-
-
Save progreccor/1fee17b6367f58a641e914b56ea0af34 to your computer and use it in GitHub Desktop.
Ajax request for joomla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let request = new XMLHttpRequest(), | |
formData = new FormData(); // Перадаем <form> или просто добавляем ниже через append что нужно | |
// Отравляем запрос | |
request.open('POST', 'url'); | |
request.send(new URLSearchParams(formData)); | |
// Отлавливаем состаянии | |
request.onreadystatechange = function () { | |
// Запрос запвершился и ответ 200 | |
if (this.readyState === 4 && this.status === 200) { | |
// Чекаем что в ответе json | |
let response = false; | |
try { | |
response = JSON.parse(this.response); | |
} catch (e) { | |
response = false; | |
console.error(request.status + ' ' + request.message); | |
} | |
if (response) { | |
// Error если joomla отдает не success | |
if (!response.success) { | |
console.error(response.message); | |
} | |
// Говорим что страница обновилась. | |
document.dispatchEvent(new Event('DOMContentLoaded', {'bubbles': true})); | |
} else { | |
console.error(request.status + ' ' + request.message); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment