1

I try to return all the records from my database when I go to the browser I get this error.

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 3 column 1 of the JSON data

I do not see an error in this record anywhere, or someone is able to help me solve this problem.

My code

<?php declare(strict_types=1);
    header('Content-Type: application/json');

    include_once("./controllers/database.php");

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

    $jsonData = array();
    while ($array = mysqli_fetch_row($res)) {
        $jsonData[] = $array;
    }
    echo json_encode($jsonData);
    mysqli_close($conn);
?>
Navro
  • 145
  • 1
  • 6
  • 1
    You need to share the json that is giving the error. – match Jan 04 '22 at 22:22
  • By any chance does your page have any other content in it, after what you've shown? – ADyson Jan 04 '22 at 22:30
  • 1
    Don't use the closing `?>` tag unless you explicitly _want_ extra plaintext data at the end of your responses. – Sammitch Jan 04 '22 at 22:34
  • Please post the offending JSON data so we can help you. – Maritim Jan 04 '22 at 22:48
  • This code return all records from database but in plain text no json. I want to only response data and catch this on the Front-end. But frist of all a working On backend – Navro Jan 04 '22 at 22:58
  • The code you've shown will return text formatted as JSON. Are you saying there is also some other code in the same script which also outputs the data in a different text format? If you're returning a JSON response, you need to ensure that your script outputs _nothing_ except the JSON. – ADyson Jan 05 '22 at 08:06

0 Answers0