jQuery ajax php 长轮询
我一直在尝试让这个长轮询脚本正常工作,但我一直陷入困境。这次向服务器发送ajax请求出现了问题。提供数据的 php 脚本(使用循环和睡眠运行长轮询)工作得很好,但我的 jQuery 脚本却不行。
到目前为止,我已经尝试对脚本使用正常的 $.ajax()
请求和 $.post()
请求,但是当我这样做时,整个脚本都会停止。好吧,脚本实际上像预期的那样加载,但页面显示它一直在加载(使用谷歌浏览器,它在屏幕底部显示“等待”栏)。在 php 脚本中 60 秒超时后,jQuery 脚本收到“false”,因为没有找到数据。然后脚本应该再次请求数据。
我的问题是:如何避免浏览器显示它正在后台加载某些内容?我的服务器是共享服务器,因此在服务器本身上安装东西时我无权访问,因此必须仅使用 javascript (jQuery) 和 php 来解决这个问题。
编辑:刚刚在 Internet Explorer 中检查了我的脚本,它似乎没有显示任何脚本正在加载的迹象,但在 Google Chrome 中仍然存在问题
编辑 发布一些代码:
这是我的 javascript文件:
if($("#interestboard"))
{
var lasttime = $("#lasttime").val();
var pollurl = $("#pollurl").val();
lpStart();
}
function lpStart()
{
$.ajax({
type: "GET",
url: pollurl+lasttime,
async: true,
cache: false,
success:function(data)
{
alert(data);
},
error: function()
{
alert("Something went wrong");
}
});
}
当然,这是包含在 $(document).ready()
中
我的 php 脚本如下所示:
while(time()-$time<60)
{
$this->class = $class["id"];
$this->orderBy("timestamp");
$this->_extraConditions = "`timestamp`>'$lastTime' AND ";
$result = $this->search();
if($result)
return $result;
usleep(250000);
clearstatcache();
}
当使用 firebug (使用 Google Chrome)时,它只显示已发出请求但不显示通常的加载图标在控制台中 GET 字符串的末尾。在 Firefox 中,它也只是显示请求正在等待(显示加载图标),但至少浏览器的行为不像页面正在加载。
谢谢...
I've been trying to get this long polling script to work but I keep getting stuck. This time I have a problem with the ajax request to the server. The php script that serves the data (running the long-poll using a loop and sleep) works perfectly fine but my jQuery script doesn't.
So far I've tried using a normal $.ajax()
request and a $.post()
request to the script however when I do so the entire script stalls. Well the script actually loads like it is supposed to but the page shows that it keeps loading (using google chrome it shows the "waiting for" bar at the bottom of the screen). After the 60 second timeout in the php script the jQuery script receives "false" because no data was found. The script is then supposed to ask for the data agian.
My question is: how do I avoid the browser showing that it's loading something in the background? My server is a shared server so I don't have access when it comes to installing stuff on the server itself so this have to be solved using only javascript (jQuery) and php.
EDIT: just checked my script out in Internet Explorer and it doesn't seem to be showing any signs that the script is loading but it's still a problem in Google Chrome
EDIT Posting some code:
This is my javascript file:
if($("#interestboard"))
{
var lasttime = $("#lasttime").val();
var pollurl = $("#pollurl").val();
lpStart();
}
function lpStart()
{
$.ajax({
type: "GET",
url: pollurl+lasttime,
async: true,
cache: false,
success:function(data)
{
alert(data);
},
error: function()
{
alert("Something went wrong");
}
});
}
Of course this is wrapped within the $(document).ready()
My php script is something like the following:
while(time()-$time<60)
{
$this->class = $class["id"];
$this->orderBy("timestamp");
$this->_extraConditions = "`timestamp`>'$lastTime' AND ";
$result = $this->search();
if($result)
return $result;
usleep(250000);
clearstatcache();
}
When using firebug (with Google Chrome) it only shows that a request has been made but not showing the usual loading-icon at the end of the GET string in the console. In Firefox it also just shows that a request is pending (showing the loading icon) but at least the browser doesn't act like the page is loading.
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该阅读 Chrome AJAX 页面上的置顶评论-load 导致“忙碌光标”保持 - 似乎这是 Google Chrome 特有的,您需要等待所有内容加载后再进行 ajax 调用。
编辑:只是一个建议 - 也许我错了:您不应该使用 GET 请求在您的网站上进行任何大型/敏感操作,因为它可能会导致对您的网站的轻松攻击。任何拥有/攻击某个高负载网站的人都可以在他们的页面中插入一个不可见的 iframe,这样任何访问该页面的用户都会在没有意识到的情况下发出 GET 请求 - 它可能会崩溃/损害您的服务器(因为成千上万的请求到达您的服务器)服务器)。大多数现代浏览器不会允许跨站点 POST 请求,因此它应该是安全的。
编辑2:关于您的网站不可用,而该脚本即使在您中止后仍然有效:这很可能是由于会话。标准会话行为将只允许一个具有相同会话的 php 实例同时运行 - 所有其他请求将等待,直到该会话在所有其他脚本中关闭。您应该在脚本中尽快关闭会话,使用自定义会话处理将启用同一会话的多个请求(很可能这会很难看)或 检测用户abort 在用户在浏览器中中止脚本后立即关闭脚本。
I think you should read top comment on Chrome AJAX on page-load causes "busy cursor" to remain - it seems it is Google Chrome specific and you need to wait before ALL content is loaded before making an ajax call.
EDIT: Just a recommendation - maby I am wrong: you should not use GET requests to make any huge/sensitive operations on your site as it can lead to and easy attack on your site. Anyone who owns/hacks some high-load site can insert an invisible iframe to their page so that any user that visits that page will make that GET request without even realising it - it can crash/harm your server (as thousands requests comes to your server). Most modern browsers will not let cross-site POST requests, so it should be safe.
EDIT 2: About your site being unavaliable while that script is still working even after you abort it: it is most likely because of sessions. Standard session behaviour will let only one php instance with same session running at the same moment - all other requests will wait untill that session is closed in all other scripts. You should close the session ASAP in script, use custom session handling that will enable multiple requests with the same session(most likely this will be really ugly) or detect user abort to close the sript as soon as user aborted it in browser.