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(不要告诉我有关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.是否有可能在1s的多个单独数据包中打印出这个结果,而不是在最终输出之前产生100000000 1s?
解决方法
请参阅此SO问题以了解可能的方法,尽管它不是Perl特定的:
Dealing with incremental server response in AJAX (in 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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。