如何使用 Ajax 显示 php 循环的步骤? (长轮询...)
我有一个 PHP 服务器端脚本,在 foreach 循环内,通过 SSH 连接到路由器并执行一些配置块。
<?php
function apply($hostname){
$ssh = new Net_SSH2($hostname);
if (!$ssh->login($this->username, $this->password)) {
exit('Login Failed');
}
$i = 1;
foreach($NMConf->configuration as $conf_step){
$ssh->write($conf_step); //Can take 5s...
$this->ssh_errors[] = $ssh->read("#");
echo "Step".$i." ok.. <br/>";
$i++;
flush();
sleep(1);}
} ?>
这个函数由 jQuery 和 Ajax 请求调用...
$.ajax({
url:'apply',
type:'POST',
cache:false,
async:false,
data:{},
success: function(data){
console.log(data);
$('#myDiv').append(data); // *_*
}
});
我想在我的输出 div 中显示不同的步骤,例如...
Step 1 (working....)
然后
Step 1 OK!
Step 2 (working ....)
然后
Step 1 OK!
Step 2 OK!
我该怎么做?
我查看了长轮询的内容&反向ajax,不过好像有点棘手。
任何想法!
谢谢
I have a PHP server-side script which, inside a foreach loop, connects to a router through SSH and executes some configuration blocks.
<?php
function apply($hostname){
$ssh = new Net_SSH2($hostname);
if (!$ssh->login($this->username, $this->password)) {
exit('Login Failed');
}
$i = 1;
foreach($NMConf->configuration as $conf_step){
$ssh->write($conf_step); //Can take 5s...
$this->ssh_errors[] = $ssh->read("#");
echo "Step".$i." ok.. <br/>";
$i++;
flush();
sleep(1);}
} ?>
This fonction is called by jQuery and an Ajax request...
$.ajax({
url:'apply',
type:'POST',
cache:false,
async:false,
data:{},
success: function(data){
console.log(data);
$('#myDiv').append(data); // *_*
}
});
I would like to display into my output div the different steps, like..
Step 1 (working....)
then
Step 1 OK!
Step 2 (working ....)
and then
Step 1 OK!
Step 2 OK!
How can I do that?
I had a look on long polling stuff & reverse ajax, but it seems a bit tricky.
Any ideas!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是,将信息放入会话中,并尝试向 php 脚本询问状态
创建将读取会话的脚本
更新现有脚本,以在完成或处理会话时将数据放入会话中。
更新您的 JavaScript 以每秒或您想要的时间执行,例如
,完成所有步骤后,只需清除间隔_int
clearInterval(_int)
What I propose, info of stuff put in session, and try to ask php script for status
Create script which will read session
Update your existing script to put data into session when done or working on it.
Update your javascript to execute every second or time you want e.g.
After all steps are done just clear interval _int
clearInterval(_int)