当我实现聊天功能时,我使用java脚本在文件之间发送数据到另一个。 但我遇到了一些问题。
代码:
lastReceived=0;
var xmlhttp;
// Sign in and Out
function signInOut(){
// Sign in
if (signInForm.signInButt.name=="signIn"){
data="user=" + signInForm.userName.value +"&oper=signin"
Requst ("POST","users.php",data,checkSignIn)
return false
}
}
// Sign out
function singout(){
data="user=" + "" +"&oper=signout"
Requst ("POST","users.php",data,checkSignOut)
return false;
}
// Sign in response
function checkSignIn(res){
if(res=="userexist"){
checkSignOut("signout");
checkSignIn("signin");
return false;
}
if(res=="signin"){
messageForm.message.focus()
updateInterval=setInterval("updateInfo()",3000);
serverRes.innerHTML="Sign in"
}
}
// Sign out response
function checkSignOut(res){
if(res=="usernotfound"){
serverRes.innerHTML="Sign out error";
res="signout"
}
if(res=="signout"){
clearInterval(updateInterval)
serverRes.innerHTML="Sign out"
return false
}
}
// Update info
function updateInfo(){
serverRes.innerHTML="Updating"
Requst ("POST","users.php","",showUsers)
Requst ("POST","receive.php","lastreceived="+lastReceived,showMessages)
}
// update online users
function showUsers(res){
usersOnLine.innerHTML=res
}
// Update messages view
function showMessages(res){
serverRes.innerHTML=""
msgTmArr=res.split("<SRVTM>")
lastReceived=msgTmArr[1]
messages=document.createElement("span")
messages.innerHTML=msgTmArr[0]
chatBox.appendChild(messages)
chatBox.scrollTop=chatBox.scrollHeight
}
// Send message
function sendMessage(){
data="message="+messageForm.message.value+"&user="+signInForm.userName.value
serverRes.innerHTML="Sending"
Requst ("POST","send.php",data,sentOk)
}
// Sent Ok
function sentOk(res){
if(res=="sentok"){
messageForm.message.value=""
messageForm.message.focus()
serverRes.innerHTML="Sent"
}
else{
serverRes.innerHTML="Not sent"
}
}
function Requst (Action,URL,Data,Fun)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4)
eval(Fun(xmlhttp.responseText));
}
Data=encodeURI(Data);
xmlhttp.open(Action,URL,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length",Data.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(Data);
}
Send.php的代码
<?php
session_start();
$user_id=$_SESSION['user_id'];
$data=$_POST['data'];
$message=strip_tags($_POST['message']);
$message=stripslashes($message);
include 'connent.php';
$MembernameR = mysql_query("select MemberName from members where MemberID = '$user_id'");
$Membername = mysql_fetch_row($MembernameR);
$user = $Membername[0];
$room_file=file("room1.txt",FILE_IGNORE_NEW_LINES);
$room_file[]=time()."<!@!>".$user.": ".$message;
if (count($room_file)>20)
$room_file=array_slice($room_file,1);
$file_save=fopen("room1.txt","w+");
flock($file_save,LOCK_EX);
for($line=0;$line<count($room_file);$line++){
fputs($file_save,$room_file[$line]."\n");
};
flock($file_save,LOCK_UN);
fclose($file_save);
echo "sentok";
exit();
?>
我认为问题出在功能“请求”它不起作用或File Send.php中的会话
你能告诉我怎样才能修复它吗? 或者你能告诉我为什么上面的代码不能正常工作吗? :(答案 0 :(得分:0)
尝试在脚本中进行以下更改,然后重试:
您无需在Request
函数中使用eval,只需正常调用它即可。
eval(Fun(xmlhttp.responseText)); // no need to eval
Fun(xmlhttp.responseText); // try this one
第二件事,为什么要将Connection
的标题设置为close
我不认为你需要设置它,只需从代码中删除以下行。
xmlhttp.setRequestHeader("Connection", "close"); // try removing this line
希望这有帮助!