阿贾克斯用于显示数据库信息 [英] ajax for displaying database information

查看:331
本文介绍了阿贾克斯用于显示数据库信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个剧本聊天所使用的Ajax,

I have a script for chatting that Used Ajax,

我希望做一个监测脚本,以使管理员能够看到用户之间的所有交流。

I would like to do a monitoring script in order to allow the administrator to see all exchanges between users.

的事情是,我没有找到解释如何接收来自DATABSE数据没有刷新页面的任何脚本。

The thing is that I did not find any script that explain how to receive data from databse with no reload of the page.

如果有人能帮助我找到脚本或者给我一个教程来构建它,我会很高兴。

If anybody could help me to find that script or give me a tutorial to build it I'll be glad.

接收我所有的最大的尊重。

Receive All my utmost Respect.

亲切的问候。

SP。

推荐答案

您正在使用AJAX ... AJAX的整个概念是不是有重新加载页面...

You are using ajax.. the whole concept of ajax is not having to reload the page...

对于这个工作最好的工具是:

The best tools for the job are:

  • 的WebSockets。更少的开销。 2双向通信。缺点:很难实现,而不是跨浏览器
  • 服务器发送的事件。更少的开销。 1双向沟通,没有重复的请求。缺点:不能跨浏览器
  • 只是简单的Ajax轮询...效率较低,更多的开销,很多请求,但它的工作无处不在。

Socket.io(对于nodeJS书面)是一个可能是最好的工作。它利用合适的传输(的WebSockets,longpolling,服务器发送的事件,flashsockets,Ajax轮询)为您会给你提供最好的性能。

Socket.io (written for nodeJS) is a probably the best for the job. It leverages the right transport (websockets, longpolling, server-sent events, flashsockets, ajax polling) for you and will give you the best performance available.

下面是几个PHP JS实现的例子:

Here are a couple php js implementation examples:

使用服务器发送的DOM事件(不跨浏览器): 更新+/-每3秒

using Server-sent DOM events (not cross browser): updates +/- every 3s

//javascript:
var source = new EventSource('updates.php');
source.onmessage = function (event) {
  console.log(event.data);
};

//php server side:
<?php
header("Content-Type: text/event-stream\n\n");
//..perform queries and put it in $data..
echo "data: " . json_encode($data) . "\n";
?>

使用jQuery Ajax请求:将发送一个请求,每1000毫秒

using jQuery ajax request: will send a request every 1000ms

//javascript: 
var interval_id = setInterval(function(){
    $.ajax({
      type: "POST",
      url: "updates.php",
      success: function(data){
        console.log("Data: ", data);
      }
    });
}, 1000); //last param is the interval time in ms

//php server side:
<?php
//..perform queries and put it in $data..
echo json_encode($data) . "\n";
?>

使用phpWebsockets: LIB + codeSAMPLE

这篇关于阿贾克斯用于显示数据库信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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