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?
This error is returned by Javascript or PHP itself?
– Camilo Silva
The only thing out of place I noticed was
$stmt->close();
this line must be inside the blockif($_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.– rray
There are accents?
– lazyFox
You return how much data?
– adventistaam
@Camilosilva is returned when I open the page in the browser. When I get to the file, nothing returns.
– Guilherme Lirio
@lazyFox only spaces.
– Guilherme Lirio
@rray removed already. thanks!
– Guilherme Lirio
@adventistaam all 9 data.
– Guilherme Lirio
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!
– Guilherme Lirio