I want to read the JSON information received from an AJAX request.
Code .js
const xhr = new XMLHttpRequest();
xhr.open("POST", "includes/modelos/modelo-contacto.php", true);
xhr.onload = function() {
if (this.status === 200) {
const respuesta = JSON.parse(xhr.responseText);
console.log($respuesta);
}
}
xhr.send(datos);
PHP Code
$respuesta = array(
'respuesta' => 'correcto',
'datos' => array(
'nombre' => $nombre,
'empresa' => $empresa,
'telefono' => $telefono,
'id_insertado' => $statement->insert_id,
)
);
echo json_encode ($respuesta);
I'm doing a course and the code is the same as the teacher uses. I have tried to create the variable $ answer with the JSON code as such and then do an echo of the variable without the encode and I have also tried to return only "answer" = > "correct" (in case it's that I'm building the JSON badly) and still I'm still giving the same error.
I have also read that it is a problem that XAMPP usually gives but I have uploaded the files to a hosting of these free and still giving the same failure.
Let's see if anyone has happened.
Thank you very much !!