Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Console: SyntaxError: Unexpected token < in JSON at position 0

I am trying to fetch data from a MySQL database and display it (currently just logging it due to the error).

The error is : SyntaxError: Unexpected token < in JSON at position 0

My PHP code connecting to the database:

<?php

$conn = mysqli_connect('localhost', 'root', '', 'mywholeheart');

$sql = mysqli_query($conn, "SELECT * FROM `products`");

$result = mysqli_fetch_all($sql, MYSQLI_ASSOC);

exit(json_encode($result));

?>

My JS code that has the fetch request:

fetch('server.php')
.then((res) => res.json())
.then(response => {
    console.log(response)
})
.catch(error => console.log(error));

Almost every video or post on this topic has to do with spelling the place you're fetching the data from which is not what I am struggling with. They also suggest logging it as text but all that does is display my PHP code.

Does anyone know why I might be getting this error and how I would go about fixing it? I am not using any libraries or frameworks at this moment.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your server doesn't execute .php files. It just get it as a text file. what the error says about < character, it is actually the first character of your text file. So that's the real problem here.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda