当我向我的getMessages.php文件发送ajax发布请求时,它不返回任何内容。
我尝试手动设置数组值并在控制台中打印它们,这似乎可行。
getMessages.php
<?php
require_once "mysqli.php";
$data = array();
if (isset($_POST['getChat']) && !empty($_POST['getChat'])) {
$username = $_SESSION["username"];
$result = mysqli_query($conn, "SELECT msg_startuser, msg, time
FROM messages
WHERE msg_startuser = '{$username}' and msg_enduser = 'mariokiller470'
UNION
SELECT msg_startuser, msg, time
From messages
WHERE msg_startuser = 'mariokiller470' and msg_enduser = '{$username}'
order by time;
");
while ($row = mysqli_fetch_array($result)) {
$data['startuser'] = $row['msg_startuser'];
$data['msg'] = $row['msg'];
}
}
echo json_encode($data);
exit;
?>
js ajax
function getChat() {
$.ajax({
url: 'getMessages.php',
type: 'POST',
data: {getChat: 'yes'},
dataType: 'JSON',
success: function(data) {
// testing
console.log(data.startuser, data.msg);
}
})
}
我希望它在控制台中打印出来进行测试。
答案 0 :(得分:0)
数据在循环中一次又一次地被覆盖,我想您想这样做:
$x = 0;
while ($row = mysqli_fetch_array ($result)) {
$data[$x]['startuser'] = $row['msg_startuser'];
$data[$x]['msg'] = $ row['msg'];
$x++;
}
答案 1 :(得分:0)
糟糕!
我忘了开始会话!
谢谢PatrickQ!
答案 2 :(得分:0)
这将解决问题,因为您将响应作为对象返回
更新:
You will need to initialize session
和数组的数据参数应位于if语句内
在下面尝试代码
<?php
require_once "mysqli.php";
session_start();
if (isset($_POST['getChat']) && !empty($_POST['getChat'])) {
$username = $_SESSION["username"];
$data = array();
$result = mysqli_query($conn, "SELECT msg_startuser, msg, time
FROM messages
WHERE msg_startuser = '{$username}' and msg_enduser = 'mariokiller470'
UNION
SELECT msg_startuser, msg, time
From messages
WHERE msg_startuser = 'mariokiller470' and msg_enduser = '{$username}'
order by time;
");
while ($row = mysqli_fetch_array($result)) {
$startuser = $row['msg_startuser'];
$msg = $row['msg'];
$data = array("startuser" =>$startuser, "msg" =>$msg);
}
echo json_encode($data);
exit;
}
?>
因此在ajax控制台中。这行代码可以正常工作
console.log(data.startuser, data.msg);
答案 3 :(得分:0)
嗨,您可以尝试这种方式:
php脚本:
<?php
require_once "mysqli.php";
session_start();// start the session
$data = array();
if (isset($_SESSION["username"])) {
if (isset($_POST["endUser"]) && isset($_POST["action"])) {
$case = $_POST["action"];
$endUser = $_POST["endUser"];
$username = $_SESSION["username"];
switch (case) {
case 'getChat':
$result = mysqli_query($conn, "SELECT msg_startuser, msg, time
FROM messages
WHERE msg_startuser = '{$username}' and msg_enduser = '{$endUser}'
UNION
SELECT msg_startuser, msg, time
From messages
WHERE msg_startuser = '{$endUser}' and msg_enduser = '{$username}'
order by time;
");
while ($row = mysqli_fetch_assoc($resultado)) {
if (isset($row['msg_startuser']) && isset($row['msg'])) {
$temp = array(
"user"=>$row['msg_startuser'],
"msg"=>$row['msg']
);
}
$data[] = $temp;
}
echo json_encode($data);
break;
}
}
}else {
echo "error-403";
}
?>
javascript:
function getChat() {
return $.ajax({
url: 'getMessages.php',
type: 'POST',
data: {action: 'getChat',endUser:'mariokiller470'},
dataType: 'JSON'
})
}
getChat()
.done(function(response){
console.log(response);
})
希望有帮助