I am working on parsing a json script queried through ajax from my database. I want to use what I queried (in a json format) in my javascript function "addComp()" that adds a geometric component for each building on a map. Here is the jQuery/ajax code:
$.ajax({
type: "GET",
url: "ajax_processor.php",
dataType : "json",
success:function(data){
console.log(data); //here I got what i want
var geometryElement = $.parseJSON(data);
for (var i=0; i<geometryElement.length; i++) {
addComp( geometryElement[i].bldg,
geometryElement[i].iZ,
geometryElement[i].iType,
geometryElement[i].x0,
geometryElement[i].y0,
geometryElement[i].p, ...); //parameters p1, p2, p3, p4, p5
}
}
});
The JSON script I got, queried through PHP is :
{"ID_geometryElement":"1","bldg":"1","iZ":"1","iType":"1","x0":"23","y0":"5","p1":"5","p2":"2","p3":"3","p4":"0","p5":"0"},
{"ID_geometryElement":"2","bldg":"1","iZ":"1","iType":"1","x0":"24","y0":"7","p1":"2.5","p2":"4","p3":"3.5","p4":"0","p5":"0"},
...
But that doesn't display anything on the map, and I got the following errors:
Uncaught SyntaxError: Unexpected token o jquery.js:550
jQuery.extend.parseJSON jquery.js:550
$.ajax.success index_LF.php:3725
fire jquery.js:3074
self.fireWith jquery.js:3186
done jquery.js:8253
callback jquery.js:8796
handleStateChange firebug-lite.js:18917
Does anyone know where it comes from and how to fix it ?
EDIT: on the PHP side, I got :
<?php
$host = 'localhost';
$databaseName = 'localdb';
$tableName = 'building_geometry';
$user = 'admin';
$password = 'password';
$connexion = mysql_connect($host,$user,$password);
$dbs = mysql_select_db($databaseName, $connexion);
$sql = mysql_query('SELECT * from building_geometry');
$rows = array();
while($r = mysql_fetch_assoc($sql)) {
$rows[] = $r;
}
echo json_encode($rows);
?>
but the problem is not in the php, I was parsing twice what was already in json (dataType is json).
'
or"
properly. The javascript code seems valid.