gpt4 book ai didi

javascript - 对 php 的 Ajax 请求在本地主机上工作,但不在实时服务器上工作

转载 作者:行者123 更新时间:2023-11-28 04:41:02 27 4
gpt4 key购买 nike

我是 AJAX 新手,只是在基于 php 的核心应用程序中测试两个用户 Angular 色之间的简单聊天应用程序。我的应用程序在 WAMP 上运行良好,但同一应用程序在我的实时服务器上无法运行。在实时服务器上,我在控制台中没有收到任何错误。我知道以下可能不是最佳实践,但这只是为了测试 ajax 功能,我无法弄清楚问题出在哪里。预先感谢您阅读长代码。

更新:我忘记添加发送消息正在工作,我可以在我的数据库中看到相同的内容,但刷新功能不起作用

下面是我的js

   $('#msgsend').click(function() { 

var x;
var x=document.getElementById("texty").value;
var sendDATA = new Array();
sendDATA[0] = 'sendMessage';
sendDATA[1] = '<?php echo $_GET['token']; ?>';
sendDATA[2] = x;
<?php if(isset($_SESSION['tid'])) { ?>
sendDATA[3]='tid';
<?php } else { ?>
sendDATA[3]='uid';
<?php } ?>
sendDATA[4] = '<?php echo isset($_SESSION['tid'])?$_SESSION['tid']:$_SESSION['id'];?>';
$.ajax({
type: "POST",
url: "helpers/chathelper.tpl.php",
data: {'sendDATA': sendDATA},
success: function(res){

document.getElementById("texty").value = "";

},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
});
window.setInterval(function(){
/// call your function here
var chatDATA = new Array();
chatDATA[0] = 'refreshChat';
chatDATA[1] = '<?php echo $_GET['token']; ?>';
chatDATA[2] = '<?php echo isset($_SESSION['tid'])?$_SESSION['tid']:$_SESSION['id'];?>';
<?php if(isset($_SESSION['tid'])) { ?>
chatDATA[3]='tid';
<?php } else { ?>
chatDATA[3]='uid';
<?php } ?>
$.ajax({ url: 'helpers/chathelper.tpl.php',
data: {'chatDATA' : chatDATA },
type: 'post',
dataType:'html',
success: function(data){
if(data !== '')
{console.log(data);
$('#chat-main').append(data);
$('#chat-main').animate({scrollTop: $('#chat-main').prop("scrollHeight")}, 500);

}

},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Status: " + textStatus); console.log("Error: " + errorThrown);
}
});
}, 2000);
});

下面是我的 php

$chatDATA=$_POST['chatDATA'];
$sendDATA=$_POST['sendDATA'];
$uid;$tid;$mes;$token;$action;$type;
if(isset($chatDATA))
{
$action=$chatDATA[0];
$token=$chatDATA[1];
if(!strcmp($chatDATA[3],'uid'))
$uid=$chatDATA[2];
else if(!strcmp($chatDATA[3],'tid'))
$tid=$chatDATA[2];
$type=$chatDATA[3];
}
else if(isset($sendDATA))
{
$action=$sendDATA[0];
$token=$sendDATA[1];
$mes=$sendDATA[2];
$type=$sendDATA[3];
if(!strcmp($chatDATA[3],'uid'))
$uid=$chatDATA[4];
else if(!strcmp($chatDATA[3],'tid'))
$tid=$chatDATA[4];
}

switch($action){
case "sendMessage" :
sendmessage($uid,$tid,$mes,$token,$con);
break;
case "refreshChat" :
refresh($uid,$tid,$token,$type,$con);
break;
}


function sendmessage($uid,$tid,$mes,$token,$con){
$mes=mysqli_real_escape_string($con,$mes);
$data = mysqli_query($con,"SELECT * FROM `chat` WHERE `token` = '".$token."'");
$key=mysqli_fetch_array($data,MYSQLI_BOTH);
if($tid!=null && ($key[2]==$tid))
{
$trav= getTraveler($tid,$con);
$mes=$trav[3].' : '.$mes;
$res2 = mysqli_query($con,"INSERT INTO `chat` (`uid`, `tid`, `jid`, `message`, `sender`, `unread`, `token`) VALUES('".$key[1]."','".$key[2]."','".$key[3]."','".$mes."','tid','0','".$token."')");
}
else if($uid!=null && ($key[1]==$uid))
{
$user=getUser($uid,$con);
$mes=$user[3].' : '.$mes;
$res2 = mysqli_query($con,"INSERT INTO `chat` (`uid`, `tid`, `jid`, `message`, `sender`, `unread`, `token`) VALUES('".$key[1]."','".$key[2]."','".$key[3]."','".$mes."','uid','0','".$token."')");
}

else
return false;

}


function refresh($uid,$tid,$token,$type,$con)
{

if(isset($uid))
$chat_read = mysqli_query($con,"select * from `chat_read` where `token`='" . $token . "' AND `uid`='".$uid."'");
else if (isset($tid))
$chat_read = mysqli_query($con,"select * from `chat_read` where `token`='" . $token . "' AND `tid`='".$tid."'");
$get_read = mysqli_fetch_row($chat_read);
if (isset($get_read) && isset($type) && isset($uid)) {
$read_sql = "SELECT * FROM `chat` WHERE `token`='" . $token . "' AND `id`>'" . $get_read[2] . "' ";
$unread = mysqli_query($con,$read_sql);
$html_output = "";
$all_results = array();
$last_id_updated;
while ($message = mysqli_fetch_array($unread,MYSQLI_BOTH)) {
$all_results[] = $message;
}
$size_arr=mysqli_num_rows($unread);
for ($i=0; $i < $size_arr; $i++) {

if ($all_results[$i]['sender'] == "uid")
$html_output .= '<li class="left clearfix"> <div class="chat-body1 clearfix"><p>'.$all_results[$i][message].'</p> <div class="chat_time pull-right">'.$all_results[$i][time].'</div> </div> </li>';
else if ($all_results[$i]['sender'] == "tid")
$html_output .= '<li class="left clearfix admin_chat"> <div class="chat-body1 clearfix"> <p>'.$all_results[$i][message].'</p> <div class="chat_time pull-left">'.$all_results[$i][time].'</div> </div> </li>';
$last_id_updated=$all_results[$i]['id'];
}
if($size_arr) {
if(isset($uid))
$read_update="UPDATE `chat_read` SET `last_id_uid`= '".$last_id_updated."',`uid`='".$uid."' WHERE token='".$token."'";
elseif (isset($tid))
$read_update="UPDATE `chat_read` SET `last_id_tid`= '".$last_id_updated."',`tid`='".$tid."' WHERE token='".$token."'";
$res=mysqli_query($con,$read_update);}
echo $html_output;

}
if (isset($get_read) && isset($type) && isset($tid)) {
$read_sql = "SELECT * FROM `chat` WHERE `token`='" . $token . "' AND `id`>'" . $get_read[3] . "' ";
$unread = mysqli_query($con,$read_sql);
$html_output = "";
$all_results = array();
$last_id_updated;
while ($message = mysqli_fetch_array($unread,MYSQLI_BOTH)) {
$all_results[] = $message;
}
$size_arr=mysqli_num_rows($unread);
for ($i=0; $i < $size_arr; $i++) {

if ($all_results[$i]['sender'] == "uid")
$html_output .= '<li class="left clearfix"> <div class="chat-body1 clearfix"><p>'.$all_results[$i][message].'</p> <div class="chat_time pull-right">'.$all_results[$i][time].'</div> </div> </li>';
else if ($all_results[$i]['sender'] == "tid")
$html_output .= '<li class="left clearfix admin_chat"> <div class="chat-body1 clearfix"> <p>'.$all_results[$i][message].'</p> <div class="chat_time pull-left">'.$all_results[$i][time].'</div> </div> </li>';
$last_id_updated=$all_results[$i]['id'];
}
if($size_arr) {
if(isset($uid))
$read_update="UPDATE `chat_read` SET `last_id_uid`= '".$last_id_updated."',`uid`='".$uid."' WHERE token='".$token."'";
elseif (isset($tid))
$read_update="UPDATE `chat_read` SET `last_id_tid`= '".$last_id_updated."',`tid`='".$tid."' WHERE token='".$token."'";
$res=mysqli_query($con,$read_update);}
echo $html_output;


}

}

最佳答案

我在数据库查询得到空响应时犯了一些错误,所以我再次编写了整个脚本并处理了所有 e 并得到了空响应下面是工作脚本。

我的JS

 $('#msgsend').click(function() { 

var x;
var x=document.getElementById("texty").value;
var sendDATA = new Array();
sendDATA[0] = 'sendMessage';
sendDATA[1] = '<?php echo $_GET['token']; ?>';
sendDATA[2] = x;
<?php if(isset($_SESSION['tid'])) { ?>
sendDATA[3]='tid';
<?php } else { ?>
sendDATA[3]='uid';
<?php } ?>
sendDATA[4] = '<?php echo isset($_SESSION['tid'])?$_SESSION['tid']:$_SESSION['id'];?>';
$.ajax({
type: "POST",
url: "helpers/chathelper.tpl.php",
data: {'sendDATA': sendDATA},
success: function(res){

document.getElementById("texty").value = "";

},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
});
window.setInterval(function(){
/// call your function here
var chatDATA = new Array();
chatDATA[0] = 'refreshChat';
chatDATA[1] = '<?php echo $_GET['token']; ?>';
chatDATA[2] = '<?php echo isset($_SESSION['tid'])?$_SESSION['tid']:$_SESSION['id'];?>';
<?php if(isset($_SESSION['tid'])) { ?>
chatDATA[3]='tid';
<?php } else { ?>
chatDATA[3]='uid';
<?php } ?>
$.ajax({ url: 'helpers/chathelper.tpl.php',
data: {'chatDATA' : chatDATA },
type: 'post',
dataType:'html',
success: function(data){
if(data !== '')
{console.log(data);
$('#chat-main').append(data);
$('#chat-main').animate({scrollTop: $('#chat-main').prop("scrollHeight")}, 500);

}

},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Status: " + textStatus); console.log("Error: " + errorThrown);
}
});
}, 2000);
});

下面是 PHP

session_start();
require_once('../includes/config.php');
require_once('../includes/db_connect.php');
require_once('../sources/functions.php');
require_once('dbhelper.php');

$chatDATA=$_POST['chatDATA'];
$sendDATA=$_POST['sendDATA'];
$uid;$tid;$id;$mes;$token;$action;$type;
if(isset($chatDATA))
{
$action=$chatDATA[0];
$token=$chatDATA[1];
$id=$chatDATA[2];
$type=$chatDATA[3];

}
else if(isset($sendDATA))
{
$action=$sendDATA[0];
$token=$sendDATA[1];
$mes=$sendDATA[2];
$type=$sendDATA[3];
if(!strcmp($sendDATA[3],'uid'))
$uid=$sendDATA[4];
else if(!strcmp($sendDATA[3],'tid'))
$tid=$sendDATA[4];
}

switch($action){
case "sendMessage" :
sendmessage($uid,$tid,$mes,$token,$con);
break;
case "refreshChat" :
refresh($id,$token,$type,$con);
break;
}


function sendmessage($uid,$tid,$mes,$token,$con){
$mes=mysqli_real_escape_string($con,$mes);
$data = mysqli_query($con,"SELECT * FROM `chat` WHERE `token` = '".$token."'");
$key=mysqli_fetch_array($data,MYSQLI_BOTH);
if($tid!=null && ($key[2]==$tid))
{
$trav= getTraveler($tid,$con);
$mes=$trav[3].' : '.$mes;
$res2 = mysqli_query($con,"INSERT INTO `chat` (`uid`, `tid`, `jid`, `message`, `sender`, `unread`, `token`) VALUES('".$key[1]."','".$key[2]."','".$key[3]."','".$mes."','tid','0','".$token."')");
echo $mes;
}
else if($uid!=null && ($key[1]==$uid))
{
$user=getUser($uid,$con);
$mes=$user[3].' : '.$mes;
$res2 = mysqli_query($con,"INSERT INTO `chat` (`uid`, `tid`, `jid`, `message`, `sender`, `unread`, `token`) VALUES('".$key[1]."','".$key[2]."','".$key[3]."','".$mes."','uid','0','".$token."')");
echo $mes;
}

else
return false;

}


function refresh($id,$token,$type,$con)
{
if(isset($id)&&isset($token)&&isset($type)&&isset($con))
{
$chat_sql="select * from `chat_read` where `token`='" . $token . "'";
$chat_read = mysqli_query($con,$chat_sql);
$get_read = mysqli_fetch_row($chat_read);
if (isset($get_read) && isset($type) && isset($id)) {

if(isset($_SESSION['id']))
$read_sql = "SELECT * FROM `chat` WHERE `token`='" . $token . "' AND `id`>'" . $get_read[5] . "' ";
else if(isset($_SESSION['tid']))
$read_sql = "SELECT * FROM `chat` WHERE `token`='" . $token . "' AND `id`>'" . $get_read[6] . "' ";

$unread = mysqli_query($con,$read_sql);
$html_output = "";
$all_results = array();
$last_id_updated=0;
while ($message = mysqli_fetch_array($unread,MYSQLI_BOTH)) {
$all_results[] = $message;
}

$size_arr=mysqli_num_rows($unread);
for ($i=0; $i < $size_arr; $i++)
{

if ($all_results[$i]['sender'] == "uid")
{ $html_output .= '<li class="left clearfix"> <div class="chat-body1 clearfix"><p>'.$all_results[$i][message].'</p> <div class="chat_time pull-right">'.$all_results[$i][time].'</div> </div> </li>';}
else if ($all_results[$i]['sender'] == "tid")
{ $html_output .= '<li class="left clearfix admin_chat"> <div class="chat-body1 clearfix"> <p>'.$all_results[$i][message].'</p> <div class="chat_time pull-left">'.$all_results[$i][time].'</div> </div> </li>';}
$last_id_updated=$all_results[$i]['id'];
}
$read_update;
if($size_arr) {
if(isset($_SESSION['id']))
{
$read_update="UPDATE `chat_read` SET `last_id_uid`= '".$last_id_updated."' WHERE token='".$token."'";
}
else if(isset($_SESSION['tid']))
{
$read_update="UPDATE `chat_read` SET `last_id_tid`= '".$last_id_updated."' WHERE token='".$token."'";
}
$res=mysqli_query($con,$read_update);}
if($res)
echo $html_output;


}

}
}

关于javascript - 对 php 的 Ajax 请求在本地主机上工作,但不在实时服务器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43769303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com