我需要返回一个 geojson 数据来响应ajax请求。
这是我的ajax代码:
postData="{\"Title\":\""+title.getText().toString()
+(getIntent().hasExtra(Common.REQID)?"\",\"ID\":\""+getIntent().getExtras().getString(Common.REQID):"")//checking update or insert
+"\",\"ReqDateFormatted\":\""+date.getText().toString()
+"\",\"ReqForCompany\":\""+companyList.get(companySpinner.getSelectedItemPosition())[0]//Company code
+"\",\"RequisitionBy\":\""+employeeList.get(employeeSpinner.getSelectedItemPosition()==0? -1:employeeSpinner.getSelectedItemPosition()-1)
+"\",\"ReqStatus\":\"Open\"" +
",\"RequisitionDetailList\":" +
detailItemsJSON
+",\"userObj\":{\"UserName\":\""+userName+"\"}}";
这是 database_query.php 由ajax触发的php文件:
$.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)
}
});
我得到的错误信息:
<?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;
}
?>
我想念或弄错了什么?
答案 0 :(得分:2)
要返回json作为响应,请不要手动创建它,而是创建一个普通的php数组并执行
echo json_encode($data);
将json返回到您的ajax调用
答案 1 :(得分:1)
json_encode
也有few optoins for encoding,这使您免于头痛。
手工字符串中的问题很可能是您使用了句点(在编码奇怪的事情发生时)。
您可以使用类似的方法来确保浮点数正确解析:
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES);