1

I need to return a geojson data in response of an ajax request.

this is my ajax code :

$.ajax({
    url : 'database_query.php',               
    data:'begin='+$("#begin").val(),
    type : 'GET',
    dataType : 'json',
    success : function(code_json, statut){
        alert("success" +" "+code_json+" "+statut)
    },
    error : function(resultat, statut, erreur){
        alert(erreur)
    }
});

and this is database_query.php the php file triggered by ajax :

<?php
try{
    // On se connecte à MySQL

    $bdd = new PDO('mysql:host=localhost;dbname=nyc;charset=utf8', 'root', '');
} catch(Exception $e) {
    // En cas d'erreur, on affiche un message et on arrête tout
    die('Erreur : '.$e->getMessage());
}
if(isset($_GET["begin"])){
    $data={"features": [{"geometry": {"coordinates": [-73.95240784, 40.81072617], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.96302032, 40.71183395], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.84346008, 40.75595093], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.92481232, 40.75424576], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.92950439, 40.75645065], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.9250412, 40.76170349], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.90320587, 40.74570465], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.84425354, 40.72134018], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.98892975, 40.70240021], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.95552826, 40.8044548], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.95846558, 40.71715546], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.9287262, 40.75831223], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.90457916, 40.74129486], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.94830322, 40.80869675], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-73.93263245, 40.8042717], "type": "Point"}, "properties": {}, "type": "Feature"}], "type": "FeatureCollection"}
    echo $data;
}

?>

what i get as an error:

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

what i am missing or mistaken ?

2 Answers 2

2

To return json as a response, don't create it manually, but instead create a normal php array and do

echo json_encode($data);

to return the json to your ajax call

Sign up to request clarification or add additional context in comments.

1 Comment

You can do but you are creating unnecessary extra work and opening yourself up for errors. As @IgorIlic has said you can do it with a single tried and tested function, json_encode.
1

json_encode has also few optoins for encoding, which saves you from a lot of headache. The problem in your handwork string most likely is that you use a period (when encoding strange things are happening). You can use something like that to be sure float numbers parsing right:

echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.