无法处理来自小型聊天模块的Ajax请求 [英] not able to handling ajax request from a small chat module

查看:125
本文介绍了无法处理来自小型聊天模块的Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的客户端开发一个简单的聊天模块,但无法处理setInterval()

i am developing a simple chat module for my client, but not able to handle loop from setInterval()

这是我发送请求以从数据库中获取数据的js代码

Here is the js code from which i sending the request to get the data from the database

function getData(){

    jQuery.ajax({
        url: '../../get_data.php',
        data: 'to=<?php echo $_SESSION['ch_usr_id']; ?>',
        cache: false,
        success: function(data){
            jQuery('<div>'+data+'</div>').appendTo("#ch-data-con");
            setInterval("getData()", 5000);
        }
    });
}

这是get_data.php文件,它根据请求生成数据

and here is the get_data.php file which produce the data according to request

require "../../support/connection/connect_gib.php";

$to = addslashes(trim($_REQUEST['to']));

$query = "select * from chat where 
          guest_id='".mysql_real_escape_string($to)."' order by id asc";
$result = mysql_query($query);

while( $row = mysql_fetch_assoc($result) ){
    echo "<b>".$row['frm']." : </b> " .$row['message'] . "<br />";
}

我在循环中得到了结果,就像所有数据都一遍又一遍地重复一样,但是我想要新近更新的数据,请让我纠正我做错的事情.

i got the result in loop like all the data repeat again and again, but i want the newly updated data, please make me correct what i did wrong..

推荐答案

您正在jQuery中使用appendTo函数.因此所有聊天消息都将附加在旧列表之后.您可以使用.html()函数替换完整的数据.

You are using appendTo function in jQuery.. So all chat messages will be appended after old list. You can use .html() function to replace complete data.

另一种方法是仅从php端获取最后一个聊天消息,因此不会重复所有数据.您可以使用自动ID来指出最后提取的消息.

Another way is to get only last chat message from php side, so all data will not be repeated. You can use auto id to point which messages are fetched last.

这篇关于无法处理来自小型聊天模块的Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆