Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda