Unexpected token in JSON

1

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 !!

    
asked by Santiago 28.11.2018 в 08:12
source

2 answers

0

You can do it like this:
Option1:

var xhr = new XMLHttpRequest();
xhr.overrideMimeType("application/json");
xhr.open("POST", "includes/modelos/modelo-contacto.php", true);
xhr.onload = function() {
    if (this.status == 200) {
        var respuesta = JSON.parse(xhr.responseText);

        console.log(respuesta);  
    }

Option 2:

var xhr = new XMLHttpRequest();
xhr.responseType = 'json';

xhr.open("POST", "includes/modelos/modelo-contacto.php", true);

xhr.onload = function() {
    if (this.status == 200) {
        var respuesta = JSON.parse(xhr.response);

        console.log(respuesta);  
    }

Current option 3:

fetch(url)
  .then(function(response) {
    return response.json();
  })
  .then(function(data) {
    console.log(data)
  });

And in the php you have a coma of more:

$respuesta = array( 'respuesta' => 'correcto', 'datos' => array( 'nombre' => $nombre, 'empresa' => $empresa, 'telefono' => $telefono, 'id_insertado' => $statement->insert_id ) ); echo json_encode ($respuesta);
    
answered by 28.11.2018 в 09:16
0

Nothing, I have modified it with the two options and keeps giving me the same error:

const xhr = new XMLHttpRequest();
xhr.overrideMimeType("application/json");

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);
    }

And the PHP has also fixed it, although that had been a mistake when copying it here:

    $respuesta = array(
        'respuesta' => 'correcto',
        'datos' => array(
            'nombre' => $nombre,
            'empresa' => $empresa,
            'telefono' => $telefono,
            'id_insertado' => $statement->insert_id
        )
    );

Later I will try fetch but I do not understand the fault with AJAX.

Many thanks, too!

    
answered by 28.11.2018 в 10:40