我正在解析通过我的数据库中的 ajax 查询的 json 脚本。我想在我的 javascript 函数“addComp()”中使用我查询的内容(以 json 格式),该函数为地图上的每个建筑物添加一个几何组件。这是 jQuery/ajax 代码:
$.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
}
}
});
我得到的通过 PHP 查询的 JSON 脚本是:
{"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"},
...
但这并没有在地图上显示任何内容,并且出现以下错误:
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
有谁知道它来自哪里以及如何解决它?
编辑:在 PHP 方面,我得到:
<?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);
?>
但问题不在 php 中,我解析了 json 中已经存在的内容的两倍(dataType 是 json)。