如何使用jquery显示长时间运行的程序的输出

时间:2015-02-09 00:54:04

标签: jquery ajax

我想在服务器上运行一个长时间运行的进程,并将输出(实时)转到我浏览器中打开的页面。

到目前为止我已经知道了这一点:

<html>
<head>
<title>jQuery Hello World</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">

$(document).ready(function(){
 $("#msgid").html("This is Hello World by JQuery");
$.get('runner.php', function (data) {
  $('#result').html(data);
});
});

</script>

This is Hello World by HTML

<div id="msgid">
</div>
<div id="result">
</div>

</body>
</html>

它似乎运行我的脚本,但我没有看到任何输出。对于测试,你可以让runner.php做一个日志文件的尾部-F。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你走在正确的轨道上!您可以使用JS函数来轮询更新并相应地更新内容。

$.get('/api/my-long-running-process', function (data) {
  $('#result').html(data);
});

然后在您的服务器上,您需要一个返回脚本当前输出的端点(此处为/api/my-long-running-process)。现在,检索输出的方式取决于输出的存储位置。如果它是一个文件,您的服务器只能返回该文件的内容。