PHP Ajax聊天:数据一台PC到另一台PC不发送/接收



我正在创建一个基本的Php Ajax聊天应用程序。

当我在我自己的PC上使用这个基本的应用程序在跨浏览器(意味着在一次chrome和Mozilla假设两个人)工作良好。但是当我在跨PC上使用这个应用程序时,意味着一个人从一台PC上聊天,另一个人从第二台PC上聊天,然后它就不起作用了。

问题:从一台PC发送的聊天内容在第二台PC上接收但是从第二台PC(聊天回复)发送的聊天内容没有收到

 Ajax response is not coming using `set Interval` and browser is not refreshing..

代码:

J查询

setInterval(function() { 
   $.ajax({
     url: "http://192.168.1.13/naresh/ajaxchat/chatsave.php?q=getChat",
     success: function(response) {
        $("#ulShowChatContent").append(response);
         }
    });
}, 1000);
Php

function getChat(){
        $useremail  = $_SESSION['email'];
        $sqlGetUserInfo = mysql_query("select * from  users where email = '$useremail'") or die(mysql_error());
        if(mysql_num_rows($sqlGetUserInfo)>0){
            $userInfo = mysql_fetch_array($sqlGetUserInfo);
            $userId = $userInfo['id']; 
            $currentdate =  date('Y-m-d H:i:s');
            $sqlGetChatContent = mysql_query("select chat_id,chat_content,name from pvt_chat 
                                                INNER JOIN users ON pvt_chat.userid = users.id 
                                                where pvt_chat.userid != '$userId' 
                                                and receive_status = 0
                                                and send_datetime <= '$currentdate' 
                                                ORDER BY send_datetime DESC limit 1") or die(mysql_error());
            if(mysql_num_rows($sqlGetChatContent)>0) {
                $resGetChatContent = mysql_fetch_array($sqlGetChatContent);
                $receiveChatId = $resGetChatContent['chat_id'];
                echo '<li>'.$resGetChatContent['name'].' says : '.$resGetChatContent['chat_content'].'</li>';
                $sqlUpdateRecStatus = mysql_query("UPDATE pvt_chat SET receive_status = '1' WHERE chat_id ='$receiveChatId'") or die(mysql_error());
            }
        }
    }

我的问题是:PC2使用什么网页(+域名)来访问聊天?如果从他的本地主机或192.168.1.13以外的任何域/IP访问该页面,则会出现跨域问题。出于安全原因,浏览器现在会阻止对另一个域(甚至子域和端口必须是相同的IIRC)上的网页的AJAX调用。如果PC2从http://localhost/chatPage.html访问网页(例如),那么他不能在AJAX调用中请求"http://192.168.1.13"。

一些解决方案:

  • 将聊天页面托管在AJAX调用发起的同一台服务器上(以便聊天页面的域与AJAX调用的域相同)
  • 使用JSON响应并在浏览器中将其转换为HTML。当您使用JSON时,有一个解决跨域问题的解决方案,但这意味着您必须自己将JSON输出转换为HTML。您还需要确保将属性dataType: 'jsonp'放在AJAX调用中。

最新更新