抱歉,帖子较长,我想具体一点。我是冷聚变和露西的新手,所以如果我错过了一些基本的东西,请原谅我。我只是想做一个快速的 POC,但无法让它工作。
我想做的是进行页面调用,写入网页,休眠一段时间。有点心跳。我无法发生的是写入网页...直到所有 sleep 发生并且页面 cfm 文件完成处理。在过去的几天里,我进行了广泛的查找,并尝试了很多项目,但都无济于事。
从我的index.cfm lucee页面,我有一个链接可以启动新选项卡并调用我的 cfm 文件。
<a href="./pinger2.cfm" target="_blank"><img class="ploverride" src="/assets/img/Ping.png" alt="Ping Test" width="125" height="75">
这里没问题,将打开一个新选项卡,并且 pinger2.cfm 开始处理。
我希望表格标题几乎立即打印到页面,然后进行第一个调用,将结果打印到页面, sleep ,进行下一个调用,打印到页面......但它没有工作。有人知道吗? pinger2.cfm 文件中的代码为:
<cfscript>
public struct function pinger( required string url, required string verb, required numeric timeout, struct body )
{
var result = {
success = false,
errorMessage = ""
};
var httpService = new http();
httpService.setMethod( arguments.verb );
httpService.setUrl( arguments.url );
httpService.setTimeout( arguments.timeout );
if( arguments.verb == "post" || arguments.verb == "put" )
{
httpService.addParam(type="body", value=SerializeJSON(arguments.body));
}
try {
callStart = getTickCount();
var resultObject = httpService.send().getPrefix();
callEnd = getTickCount();
callLength = (callEnd-callStart)/1000;
if( isDefined("resultObject.status_code") && resultObject.status_code == 200 )
{
result.success = true;
logMessage = "Pinger took " & toString( callLength ) & " seconds.";
outLine = "<tr><td>" & resultObject.charset & "</td><td>" & resultObject.http_version & "</td><td>" & resultObject.mimetype & "</td><td>" & resultObject.status_code & "</td><td>" & resultObject.status_text & "</td><td>" & resultObject.statuscode & "</td><td>" & logMessage & "</td></tr>";
writeOutput( outLine );
getPageContext().getOut().flush();
return result;
}
else
{
throw("Status Code returned " & resultObject.status_code);
}
}
catch(Any e) {
// something went wrong with the request
writeDump( e );
abort;
}
}
outLine = "<table><tr><th>charset</th> <th>http_version</th> <th>mimetype</th> <th>status_code</th> <th>status_text</th> <th>statuscode</th> <th>time</th> </tr>";
writeOutput( outLine );
getPageContext().getOut().flush();
intCounter = 0;
while(intCounter LT 2)
{
theResponse = pinger(
url = "https://www.google.com",
verb = "GET",
timeout = 5
);
intCounter = intCounter + 1;
getPageContext().getOut().flush();
sleep(2000);
}
outLine = "</table>";
writeOutput( outLine );
</cfscript>
注意:我确信其中还有其他“不是最佳”的做法,但我只是想快速而肮脏地做到这一点。
我以为getPageContext().getOut().flush();
可以解决这个问题,但是不行。
编辑:如果重要的话,我正在使用 CF 版本 10,0,0,0 和 Lucee 版本 4.5.2.018。
最佳答案
我做了类似的事情来手动生成 ETag(使用 Lucee 4.5)。我贴一个简单的
GetPageContext().getOut().getString()
在Application.cfc 中的onRequestEnd
函数中。这将返回一个 HTML 字符串,就像发送到浏览器一样。
您可以将其存储在适当的范围(应用程序、 session 等)中并在以后使用它,或者您需要的任何内容。显然,所有处理都需要完成,但不需要任何刷新。事实上,冲洗可能会也可能不会改变其行为。
关于html - 如何在 cfscript 内将输出输出到网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47933106/