2013-01-02 67 views
0

我正在創建一個基本的PhpAjax聊天應用程序。PHP Ajax聊天:數據一臺PC到另一臺電腦不發送/接收

當我在我自己的PC上使用這個基本的應用程序在跨瀏覽器(意味着在一次鉻和Mozilla假設兩個人)工作正常。但當我在跨PC使用此應用程序意味着一個人正在聊天從一臺PC和另一名男子正在聊天從第二臺PC然後它不工作..

問題:發送來自一臺PC的聊天內容正在接收第二臺PC 但是從第二PC(聊天答覆)發送聊天內容不接收

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

代碼:

Ĵ查詢

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

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()); 
      } 
     } 
    } 
+0

PHP + MySQL對於這樣的任務是錯誤的堆棧 - 查看[Node.js + Websockets](http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial)。 – moonwave99

+0

k先生。我會應用他們,但現在爲什麼它會造成問題? –

+1

那麼檢查您的控制檯是否有任何錯誤並報告,我們無法得到任何線索。 – moonwave99

回答

2

我的問題給你:什麼網頁(+域)正在使用PC2訪問聊天?如果該頁面是從他的本地主機或除192.168.1.13以外的任何域/ IP訪問的,則說明您有跨域問題。 出於安全原因,瀏覽器今天阻止對另一個域上的網頁(甚至子域和端口必須是相同的IIRC)的AJAX調用。如果PC2從http://localhost/chatPage.html(例如)訪問網頁,則他無法在AJAX調用中向「http://192.168.1.13」發出請求。

一些解決方案:

  • 主機在同一臺服務器在您的AJAX調用源自於chatpage(這樣chatpage的領域是一樣的AJAX調用的域)
  • 使用JSON響應並在瀏覽器中將其轉換爲HTML。當您使用JSON時,有一個解決跨域問題的解決方法,但這意味着您必須自己將JSON輸出轉換爲HTML。您還需要確保將屬性dataType: 'jsonp'置於您的AJAX調用中。
+0

爵先生我使用跨瀏覽器在我的電腦上聊天時使用http:// localhost。 和http://192.168.1.13正在使用當我聊天交叉時PC –

+1

然後,你應該嘗試打開一個網絡控制檯/開發人員工具(通常通過按F12打開)並查看網絡選項卡或類似的東西。 嘗試瞭解發送AJAX請求時會發生什麼。 我仍然非常確定這是一個跨域問題(因爲它在同一臺計算機/ IP上工作,但它不在多個IP上),所以我認爲你仍應該考慮我的解決方案。 – g00glen00b

相關問題