JSON error in PHP : Syntaxerror: JSON.parse!

Asked

Viewed 152 times

0

I have a problem filling Google Maps with some markers, coming from a PHP (json).

When I open the browser, error appears:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

PHP code:

<?
   header('Content-Type: application/json');
   header('Access-Control-Allow-Origin: *');

   include 'conn-login.php';

   if($_SERVER['REQUEST_METHOD'] == 'GET'){

     $sql = "SELECT id, lat, lng, endereco, especie, raca, obs, data, id_cadastro FROM mapa";
     $stmt = $mysqli->prepare($sql);
     $stmt->execute();
     $stmt->bind_result($id, $lat, $lng, $endereco, $especie, $raca, $obs, $data, $id_cadastro);
     $stmt->store_result();


   if($stmt->num_rows <= 0){

       $var = Array(
         'status' => 'ERRO',
         'msg' => 'Nenhum dado encontrado'
       );

     } else {

       while ($ln = $stmt->fetch()){

         $var = Array(
           'status' => 'OK',
           'id' => $id,
           'lat' => $lat,
           'lng' => $lng,
           'endereco' => $endereco,
           'especie' => $especie,
           'raca' => $raca,
           'obs' => $obs,
           'data' => $data,
           'id_cadastro' => $id_cadastro
         );

       };
     };

       echo json_encode($var);
       //var_dump($var);
       exit;


   }

   $stmt->close(); 

   ?>

And with this error, Google Maps can not pull the information. Someone could help me where I am missing?

  • 1

    This error is returned by Javascript or PHP itself?

  • 1

    The only thing out of place I noticed was $stmt->close(); this line must be inside the block if($_SERVER['REQUEST_METHOD'] == 'GET'){ if it will not generate an error since the connection is only opened if you enter if, if you do not enter there is no valid one. Also remove ; after the keys.

  • 1

    There are accents?

  • 1

    You return how much data?

  • @Camilosilva is returned when I open the page in the browser. When I get to the file, nothing returns.

  • @lazyFox only spaces.

  • @rray removed already. thanks!

  • @adventistaam all 9 data.

  • I managed to fix it. My . php file must have some bug. I took a newer one and reworked it to what I want and it worked!

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.