Error returning array to ajax by php

I generate an array through php and echo it back. Ajax enters the datatype of error (ajax and sets it to json),. I infer that the reason is that it is not returned in json format.

ask the Great God to have a look, thank you!

ajax section:

$("-sharpricotext").keyup(function(){
                $.ajax({
                  type:"post",
                  url:"search.php",
                  dataType:"json",
                  data:{search:$("-sharpricotext").val()},
                  success:function(feedbackdata)
                    {
                      console.log(feedbackdata);
                      console.log(success);
                    },
                  error:function(feedbackdata)
                    {
                      console.log(feedbackdata);
                      console.log(error);
                    },
                });
            });

php section:

<?php
        $servername = "localhost";
        $username = "root";
        $password = "root";
        $dbname = "guitartabs";
        $search=$_POST["search"];
         
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die(": " . $conn->connect_error);
        } 
         
        $sql = "SELECT * FROM tabs where name like "%$search%" OR singer like "%$search%"";
        $result = $conn->query($sql);
         
        $num_results = $result->num_rows;

        if ($result->num_rows > 0) {
         //
            $backresults=array();
            for($i=0;$i<$num_results;$iPP){
                $row = $result->fetch_assoc();
         //
                array_push($backresults,array("name"=>$row["name"],"singer"=>$row["singer"],"address"=>$row["address"]));
            };
         //ajax
             echo(json_encode($backresults));
        } else {
            echo "";
        }
        $conn->close();
    ?>

the data received by the chrome browser is:

[{"name":"NightWish","singer":"NightWish","address":"uploads\/NightWish.gp5"},{"name":"night","singer":"rico","address":"uploads\/MultiTrack.gp5"},{"name":"nightbar","singer":"ricoq","address":"uploads\/Serenade.gp5"}]

print simultaneously with console.log

readyState: 4
error

indicates that you have entered error.
I think if the echo is a json object, it should solve the problem.
can you tell me how to solve this problem?

Mar.07,2021

add header before echo

header('Content-type: application/json');
echo(json_encode($backresults));
...

echo (json_encode ($backresults)); eixt ();) Or return json_encode ($backresults), I am usually return $backresults, then console.log () is an object, and it is also convenient to deal with


I guess this may be a queue problem
because in ajax, before your request gets a response from the server, execute the following statement
you can try to set async:flase-> wait for the server to return data before executing


I usually write as:

  JSON predefined constant  

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b32f30-2be4a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b32f30-2be4a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?