【问题标题】:jQuery ajax get calljQuery ajax 获取调用
【发布时间】:2015-02-13 20:48:51
【问题描述】:

我无法弄清楚我的 ajax 调用出了什么问题,我只是想从我的 php 文件中显示 json。非常感谢任何帮助。

PHP (phonecall.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 标签中没有显示任何内容。有什么想法吗?

【问题讨论】:

  • 你看过浏览器控制台中的请求/响应吗?

标签: javascript php jquery ajax mysqli


【解决方案1】:

您正在多次写入数组,只需执行一次,检查您的控制台以检查它的响应是否正确。

$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);

【讨论】:

    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    相关资源
    最近更新 更多