在 Perl CGI 中分段 AJAX 响应?

发布于 2024-09-11 20:59:47 字数 853 浏览 5 评论 0原文

perl cgi 脚本是否可以将其 AJAX 响应分段为多个单独的 HTTP 响应?

假设我有这段代码:

xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        onDataReceived(xmlhttp.responseText);
    }
    else if(xmlhttp.status!=200 && xmlhttp.status!=0) {    }
}
xmlhttp.open("POST","script.cgi",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(toURLString(options));

as javascript(不要告诉我有关 ie 的 xml 对象兼容性问题,我知道,并且不在乎)。

这是:

print "Content-type: text/html\n\n";

my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
    $|=1;
    for(my $i, (1..100000000))
    {
        print "1\n";
    }
}

作为 perl cgi。是否有可能以许多单独的 1 数据包的形式打印出这个结果,而不是在最终输出之前生成 100000000 个 1?

Is it possible for a perl cgi script to segment its AJAX responses into numerous individual HTTP responses?

Say I have this code:

xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        onDataReceived(xmlhttp.responseText);
    }
    else if(xmlhttp.status!=200 && xmlhttp.status!=0) {    }
}
xmlhttp.open("POST","script.cgi",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(toURLString(options));

as javascript (dont tell me about xml object compatibility issues with ie, I know, and don't care).

and this:

print "Content-type: text/html\n\n";

my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
    $|=1;
    for(my $i, (1..100000000))
    {
        print "1\n";
    }
}

as perl cgi. Is it possible to print out this result in numerous individual packets of 1s, instead of generating 100000000 1s before finally having an output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-09-18 20:59:47

请参阅此SO问题以了解可能的方法,尽管它不是特定于Perl的:

处理 AJAX 中的增量服务器响应(在 JavaScript 中)

从链接的 Wiki 文章中,此链接似乎最相关:http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest

但是,我强烈建议考虑采用轮询方法而不是“服务器推送”您正在考虑的一个:

服务器将数据块存储为可访问的文件(带有一些排序元信息)

print "Location: xxxx"; 
# Sorry, forgot the exact form of Location HTTP response.
# Location points to URL mapped to /home/htdocs/webdocs/tmp/chunk_0.html
my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
    $|=1;
    $file_num = 0;
    my $fh;
    for(my $i, (1..100000000))
    {
        if ($i % 1000 == 0) {
            close $fh if $fh;
            open $fh, ">", "/home/htdocs/webdocs/tmp/chunk_${file_num}.html";
            # Add the usual error handling on open/close i'm too lazy to type
            $file_num++;
        }
        print $fh "1\n";
    }
    print $fh "\n##############END_TRANSMISSION__LAST_FILE####################\n";
    # This was a singularly dumb way of marking EOF but you get the drift
    close $fh;
}

AJAX 轮询器在循环中逐个检索它们,处理包含下一个块的响应并查找让元信息知道下一个要轮询的部分是什么(以及是否)。

Please see this SO question for possible approaches, though it's not Perl specific:

Dealing with incremental server response in AJAX (in JavaScript)

From the linked Wiki article, this link seems most relevant: http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest

However, I would strongly suggest considering a polling approach instead of the "server push" one you are considering:

The server stores the chunks of data as accessible files (with some ordering meta info)

print "Location: xxxx"; 
# Sorry, forgot the exact form of Location HTTP response.
# Location points to URL mapped to /home/htdocs/webdocs/tmp/chunk_0.html
my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
    $|=1;
    $file_num = 0;
    my $fh;
    for(my $i, (1..100000000))
    {
        if ($i % 1000 == 0) {
            close $fh if $fh;
            open $fh, ">", "/home/htdocs/webdocs/tmp/chunk_${file_num}.html";
            # Add the usual error handling on open/close i'm too lazy to type
            $file_num++;
        }
        print $fh "1\n";
    }
    print $fh "\n##############END_TRANSMISSION__LAST_FILE####################\n";
    # This was a singularly dumb way of marking EOF but you get the drift
    close $fh;
}

The AJAX poller retrieves them in a loop one by one, processing the response containing the next chunk and looking for meta-info to know what (and if) the next piece to poll for is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文