我试图以 json 返回 ajax 响应,但是当我在日志中打印它时,即使表有行,它也会给出 null, 我的 php 代码是:
if(isset($_GET['proid'])){
$projid = $_GET['proid'];
include(db.php);
$res = mysqli_query($con, "SELECT * FROM data WHERE project_id LIKE '%$projid%'");
while($row = mysqli_fetch_assoc($res))
{
$dataarray[] = $row;
}
echo json_encode($dataarray);
}
ajax:
$.ajax({
url : 'getRecStudy.php',
type : 'GET',
data : {proid:study},
success : function(data) {
$('#tbody').empty();
$("#tbody").append(data);
console.log(data);
}
});
怎么了?
最佳答案
除了变量之外,我发现您的代码没有任何问题。您需要调试php文件中的代码
if(isset($_GET['proid'])){
echo $_GET['proid'] . " is proid";
$projid = $_GET['proid'];
include(db.php);
echo "db connected";
$res = mysqli_query($con, "SELECT * FROM data WHERE project_id LIKE '%$projid%'");
echo "result fetched";
while($row = mysqli_fetch_assoc($res))
{
$dataarray[] = $row;
echo "inside while";
}
echo json_encode($dataarray);
print_r($dataarray);
exit;
}
在这一切之后http://yourdomain.com/yourfile.php?proid= Correctvalue
你会遇到这个错误。
关于php - 以 json 形式返回表行并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304603/