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));
作为 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?
请参阅这个SO问题以了解可能的方法,尽管它不是特定于Perl的:
在 AJAX 中处理增量服务器响应(在 JavaScript 中) https://stackoverflow.com/questions/1155066/dealing-with-incremental-server-response-in-ajax-in-javascript
从链接的 Wiki 文章来看,此链接似乎最相关:http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest 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 轮询器在循环中逐一检索它们,处理包含下一个块的响应并查找元信息以了解下一个要轮询的块是什么(以及是否)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)