I can't receive data from AJAX request in my ajax.php file:
Here my AJAX request
window.addEventListener("DOMContentLoaded", (event) => {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'src/ajax.php', true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
console.log(response);
}
};
var test = { data: "test",};
xhr.send(JSON.stringify(test));
});
And here the ajax.php :
echo json_encode($_POST['test']);
I choose to not use jQuery by the way because I don't want to.