Ajax请求php在localhost上工作,但不在实时服务器上

时间:2017-05-03 20:21:09

标签: javascript php ajax

我是AJAX的新手,只是在基于核心php的应用程序中测试两个用户角色之间的简单聊天应用程序。我的应用程序在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;


}

}

0 个答案:

没有答案