jQuery Ajax PHP长轮询 [英] jQuery ajax php long polling

查看:107
本文介绍了jQuery Ajax PHP长轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使这个冗长的轮询脚本正常工作,但我一直陷于困境.这次我对服务器的ajax请求有问题.提供数据的php脚本(使用循环和睡眠来运行长轮询)工作得很好,但是我的jQuery脚本却不能.

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.

到目前为止,我已经尝试对脚本使用普通的$.ajax()请求和$.post()请求,但是当我这样做时,整个脚本将停滞.好吧,脚本实际上按照预期的方式加载,但是页面显示它一直在加载(使用谷歌浏览器,它在屏幕底部显示了"waiting for"栏). php脚本中的60秒超时后,jQuery脚本收到"false",因为未找到数据.然后,该脚本应要求提供数据agian.

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.

我的问题是:如何避免浏览器显示它正在后台加载内容?我的服务器是一台共享服务器,因此在服务器本身上安装东西时我没有访问权限,因此只能使用javascript(jQuery)和php来解决.

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.

只是在Internet Explorer中签出了我的脚本,它似乎没有任何迹象表明该脚本正在加载,但是在Google Chrome浏览器中仍然是一个问题

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

编辑:发布一些代码:

这是我的javascript文件:

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");
        }
    });
}

当然,它包装在$(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中,它也仅显示请求处于待处理状态(显示正在加载图标),但至少浏览器不会像页面正在加载那样运行.

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.

谢谢...

推荐答案

我认为您应该阅读-似乎它是特定于Google Chrome的,您需要等待 ALL 内容加载后再进行Ajax调用.

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.

这只是一个建议-老兄,我错了:您不应该使用GET请求在您的网站上进行任何庞大/敏感的操作,因为它可能导致对您的网站的轻松攻击.拥有/入侵某些高负载网站的任何人都可以在其页面上插入一个不可见的iframe,这样,访问该页面的任何用户都将发出该GET请求,甚至没有意识到它-它可能导致服务器崩溃/损坏(成千上万的请求进入您的服务器服务器).大多数现代浏览器都不允许跨站点POST请求,因此应该是安全的.

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.

关于您的站点即使该脚本中止后仍无法正常工作的情况下仍然不可用:这很可能是由于会话所致.标准会话行为将只允许一个具有相同会话的php实例在同一时间运行-所有其他请求将等待,直到在所有其他脚本中该会话关闭为止.您应该关闭会话,并在脚本中尽快使用自定义会话处理将允许在同一会话中启用多个请求(最有可能是非常丑陋的)或

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.

这篇关于jQuery Ajax PHP长轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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