jQuery 使用 AJAX 观察 SQL 数据库的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2813353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use AJAX to watch SQL database for changes
提问by Jacob Huggart
I have a column in my database that will be updated randomly. Whenever that column is updated I need to refresh content on my page. How can I use AJAX + jQuery to perform an action only on DB changes?
我的数据库中有一个列将随机更新。每当该列更新时,我都需要刷新页面上的内容。如何使用 AJAX + jQuery 仅对数据库更改执行操作?
采纳答案by Pranay Rana
use setinterval function of javascript for polling and to check the value of the updated dababase field
使用 javascript 的 setinterval 函数进行轮询并检查更新后的 dababase 字段的值
check following link for more detail http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
检查以下链接以获取更多详细信息http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
回答by Robert Harvey
What you are describing is colloquially called Cometprogramming. Comet describes a group of techniques for pushing content to a web page with a persistent HTTP connection.
您所描述的内容通俗地称为Comet编程。Comet 描述了一组使用持久 HTTP 连接将内容推送到网页的技术。
The push would be initiated using a trigger/stored procedure combination in the database server. That way, it occurs no matter where the data update comes from.
推送将使用数据库服务器中的触发器/存储过程组合启动。这样,无论数据更新来自何处,它都会发生。
回答by Nelson Rothermel
The server (database/web) can't initiate a connection--only the client can. So you will have to poll the database until there is an update. You can create a web service that checks the database and which jQuery uses.
服务器(数据库/网络)无法发起连接——只有客户端可以。所以你必须轮询数据库直到有更新。您可以创建一个检查数据库和 jQuery 使用的 Web 服务。
Edit:I stand corrected. It is possible to keep an AJAX connection open until the server "pushes" data to it. See: http://en.wikipedia.org/wiki/Reverse_Ajax
编辑:我站纠正。可以保持 AJAX 连接打开,直到服务器向其“推送”数据。请参阅:http: //en.wikipedia.org/wiki/Reverse_Ajax
And apparently it really is polling: http://en.wikipedia.org/wiki/Push_technology#Long_polling. If the server doesn't have any data to send yet, it keeps the connection open until it does. It's not "pure" push technology, because the client doesn't have a listening port which the server connects to. The effect is similar, however.
显然它确实是轮询:http: //en.wikipedia.org/wiki/Push_technology#Long_polling。如果服务器还没有任何数据要发送,它会保持连接打开,直到发送为止。它不是“纯粹的”推送技术,因为客户端没有服务器连接到的侦听端口。然而,效果是相似的。
Edit 2:So back to answering your question... You'll have to choose how to "poll" the web service. The web service would then have to check the database to see if there were updates. Checking the database for updates might be the trickiest and really depends on your requirements. You can run a SQL query to see if anything changed, but how would you know? You would need some sort of parameter (typically a date) to compare against. If done wrong, you could miss some updates or have multiple hits for one update. What Autocracy said would be a good way to get notified of updates. You can keep that list in the database, in memory, etc. and clear it when the client gets the updates.
编辑 2:所以回到回答你的问题......你必须选择如何“轮询”网络服务。然后,Web 服务必须检查数据库以查看是否有更新。检查数据库的更新可能是最棘手的,这实际上取决于您的要求。您可以运行 SQL 查询以查看是否有任何更改,但您怎么知道?您需要某种参数(通常是日期)进行比较。如果做错了,您可能会错过一些更新或一次更新有多次点击。独裁者所说的将是获得更新通知的好方法。您可以将该列表保存在数据库、内存等中,并在客户端获取更新时清除它。
回答by Mads Spjell
Im doing almost same thing with a chat, reloads a php script every xx sec.
我在聊天中做几乎相同的事情,每 xx 秒重新加载一个 php 脚本。
Looks like this: replace j with $ if not using jquery.noconflict..
看起来像这样:如果不使用 jquery.noconflict,请用 $ 替换 j。
j(".chatref").everyTime(3000,function(i){
j.ajax({
url: "chatx.php",
cache: false,
success: function(updated){
j(".chatref").html(updated);
...do stuff..
}
});
This is an very nice method i think :) if you want to send vars to chatx.php just add ?php &x=1&y=2?>
我认为这是一个非常好的方法:) 如果您想将 vars 发送到 chatx.php,只需添加 ?php &x=1&y=2?>
回答by Jeff Ferland
Hook a special stored procedure into your database that alerts your application after a modification. You'll need a custom module and the right trigger statements.
将一个特殊的存储过程挂接到您的数据库中,在修改后提醒您的应用程序。您将需要一个自定义模块和正确的触发器语句。
Your other option is polling.
你的另一个选择是投票。
回答by gurun8
I'd suggest creating an HTML page that uses setIntreval() to repeatedly make AJAX calls to a PHP script that queries the your database. You could use JSON and PEAR to make the task a little easier.
我建议创建一个 HTML 页面,该页面使用 setIntreval() 重复对查询数据库的 PHP 脚本进行 AJAX 调用。您可以使用 JSON 和 PEAR 使任务更容易一些。
Reference Links:
参考链接:
回答by Mitch Dempsey
You will have to continually poll a page that has the value from the database.
您将不得不不断轮询具有来自数据库的值的页面。
回答by aepheus
You will basically have to constantly poll the server for changes to the DB. The server cannot make a call to the client, so the client will just have to constantly ask the server if there have been changes.
您基本上必须不断轮询服务器以了解对数据库的更改。服务器无法调用客户端,因此客户端只需要不断地询问服务器是否有更改。