Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!