【问题标题】:How to write an Ajax code to fetch new row when it is inserted in MySQL?如何编写 Ajax 代码以在将新行插入 MySQL 时获取新行?
【发布时间】:2014-11-07 07:28:57
【问题描述】:

我有一个简单的聊天服务。我将消息、登录名和时间等存储在 Mysql 数据库中。聊天消息在 Ajax 和 PHP 的帮助下显示在下面

<div id="messages"> </div><!--- chats are displayed here -->

我有以下 Ajax 代码,它每 2 秒从 Mysql 获取数据。当然,大家建议不要这样做。它可能会对服务器性能产生负面影响。而且它是不必要的。

$(document).ready( function() {

     var destination_hashed = $("#destination_hashed").val();

     var interval = setInterval( function() {

            $.ajax ({
                     type: "POST",
                     url: "chat.php",
                     data: { destination_hashed1: destination_hashed },

                     success: function(data) {
                            $("#messages").html(data);
                     }
             });
     }, 2000);
});

简而言之,我有两个聊天客户端 A 和 B。当 A 向 B 发送消息时,Mysql 中插入了新行。

那么,我如何编写 Ajax 和 PHP 代码以仅在有新行时获取。而不是每 2 秒从 Mysql 获取数据,无论是否插入新行

【问题讨论】:

  • 当网站上的所有其他问题都以正常字体大小完成时,无需将所有文本包含在 heading3

标签: php mysql ajax


【解决方案1】:

最近我在开发这种聊天模块,我可以在你的代码中做一些更正

首先不要以你使用的方式使用setInterval

为什么

因为在您的特定代码中,请求每 2 秒发送一次到服务器,因此如果网络速度较慢,您的代码将倾向于向服务器发送大量请求,并且这些请求之间的协调会很困难。

所以你要做的是

function getNewMessage(){

            $.ajax ({
                     type: "POST",
                     url: "chat.php",
                     data: { destination_hashed1: destination_hashed },

                     success: function(data) {
                            $("#messages").html(data);
                     },
                     complete : function(){  // this will be called even if our request fail, success etc
                         setTimeout(function(){   // send request after 2 second from the complition of the previous request
                             getNewMessage();       
                         },2000);
                     }
             });

}

从ajax调用解析聊天结果有两种方法

a) 从 ajax 调用中获取 html(如果您想在未来扩展您的模块,这会很慢且效率低下)

您可以简单地从 ajax 调用 succes 参数中获取 html 内容,您可以将其附加到表格中

 For example : -

   if the response from the server on new message is 

     <div class="chat"> 
       <lable>John</lable>
         Hi there !!!
    </div>

然后解析内容会是这样的

             success: function(data) {
                    // $("#messages").html(data);   //this will change the content in every request
                    $("#messages").append(data);   // this will append the new conent
             },

b) 另一个方法是获取 json 格式的数据(更好的方法)

例如:-

   if the response from the server on new message is 

     { 
       username : 'john',
       message  : 'Hi there !!!'
     }

然后解析内容会是这样的

             success: function(data) {
                    // $("#messages").html(data);  this will change the content in every request
                    $("#messages").append('<div class="chat">'+
                                            '<lable>'+data.username+'</lable>'+
                                              data.message +
                                          '</div>');    this will append the new conent
             },  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2011-06-12
    • 2020-07-20
    相关资源
    最近更新 更多