我无法弄清楚我的ajax调用出了什么问题,我只是试图显示我的php文件中的json。任何帮助将不胜感激。
PHP(电话.php):
<?php
$con = mysqli_connect('localhost','root','root','mydb');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"mydb");
$sql="SELECT * FROM incoming_calls";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$callArray[] = array('phonenumber' => $row['phone_number'], 'id' => $row['phone_login_id']);
echo json_encode($callArray);
}
mysqli_close($con);
?>
.HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Phone calls</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
$.ajax({
type: "GET",
url: "phonecall.php",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response){
$("#call").html(response);
}
});
</script>
<div id="call"></div>
</body>
</html>
我的div 标签中没有显示任何内容。有什么想法吗?
您正在多次写入数组,只需执行一次,请检查控制台以检查响应是否正确。
$callArray = array();
while($row = mysqli_fetch_array($result)) {
$callArray[] = array('phonenumber' => $row['phone_number'], 'id' => $row['phone_login_id']);
}
mysqli_close($con);
echo json_encode($callArray);