I want to import data from the database into my javascript code, using ajax through jQuery, but I got a json parse error and I really don't know what it does come from. Could you help me ?
The goal is to build buildings on a map, and i take the geometry elements such as coordinates and shape parameters from the database.
in the JS file, put:
$.ajax({ type: "GET", url: "ajax_processor.php", dataType : "html", error:function(msg, string){ alert( "Error !: " + string ); } success:function(returnData){ var data = $.parseJSON(returnData); for(var ID_geometryElement in data) { addComp($data[bldg], $data[iZ], // zone where is the building $data[iType], //type of the geometric element $data[x0], $data[y0],//coordinates top-left $data[p], // geometric parameters ); } } });
});
in the PHP file:
try { $bdd = new PDO('mysql:host=localhost;dbname=building_geometry','admin','password'); } $reponse = $bdd->query('SELECT * FROM ID_GEOMETRYELEMENT'); $donnees = $reponse->fetch(); header('Content-Type: application/json'); echo json_encode($response); ?>