我试图在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);
}
});
怎么了?
答案 0 :(得分:1)
除了varibales之外,我的代码中没有发现任何问题。你需要调试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
你会得到这个错误。
答案 1 :(得分:0)
在像这样的
成功函数中使用parseJSON方法var obj = jQuery.parseJSON( data );
alert( obj.name ); // For example name is a key