I am creating a basic Php Ajax chat application.
when i am using this basic application on my own PC in cross browser(means in one time chrome and Mozilla assuming two person) is working fine. but when i am using this application on cross PC means one man is chatting from one PC and another man is chatting from 2nd PC then it is not working..
Problem : send chat content from one PC is receiving on 2nd pc
but from second PC (chat Reply) send chat content is not receiving
Ajax response is not coming using `set Interval` and browser is not refreshing..
Code :
J query
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());
}
}
}
My question to you: what web page (+ domain) is PC2 using to access the chat? If the page is accessed from his localhost or any domain/IP other than 192.168.1.13 you have a cross-domain issue.
Browsers today block AJAX calls to webpages on another domain (and even subdomain and port has to be the same IIRC) for security reasons. If PC2 is accessing the webpage from
http://localhost/chatPage.html(for example), then he cannot make a request to “http://192.168.1.13” in the AJAX call.Some solutions:
dataType: 'jsonp'in your AJAX call.