当我打印为数组时,没关系,它打印,但它不会打印为JSON Objects
,相反,它返回空白页
提前致谢
<?
require_once('include/db_functions.php');
if($_GET){
$ad = stripslashes($_GET["ad"]);
try{
$dbFuncs = new db_functions();
}catch(PDOException $e){
print $e -> getMessage();
}
$ss = $dbFuncs->getCategoryLista($ad);
foreach($ss as $ccc){
$data[] = array(
"ID" => $ccc['CategoryID'],
"Name" => $ccc['CategoryName']
);
}
header("Content-type: application/json; charset=utf-8");
echo json_encode($data);
}
?>
由于不同的因素,它可能为空,实际上,如果一切按预期正常,您的代码只会输出 JSON。您只编写了快乐的情况,您需要在 json 中包含所有可能的条件:
<?php
header("Content-type: application/json; charset=utf-8");
require_once('include/db_functions.php');
$data = array();
if(isset($_GET["ad"])){
$ad = stripslashes($_GET["ad"]);
try{
$dbFuncs = new db_functions();
$ss = $dbFuncs->getCategoryLista($ad);
if(count($ss) != 0){
$data['success'] = true;
foreach($ss as $ccc){
$data['data'][] = array(
"ID" => $ccc['CategoryID'],
"Name" => $ccc['CategoryName']
);
}
}else{
$data['success'] = false;
$data['data'] = 'count() == 0';
}
}catch(PDOException $e){
print $e -> getMessage();
$data['success'] = false;
$data['data'] = 'error = '.$e -> getMessage();
}
}else{
$data['success'] = false;
$data['data'] = '$_GET["ad"] is not set';
}
echo json_encode($data);
?>