Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 等待脚本完成[ajax]_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 等待脚本完成[ajax]

Javascript 等待脚本完成[ajax],javascript,jquery,ajax,Javascript,Jquery,Ajax,下面的代码显示了我对run.py的ajax调用,然后在html标记中显示输出。脚本run.py运行超过2分钟。但是在下面的js脚本中。一旦脚本开始运行。输出(输出的初始行)将显示在html标记中。脚本的其余部分将不会显示 $(document).ready(function(){ $(sub).click(function(){ alert("connecting to host") $.ajax({ type:'POST

下面的代码显示了我对run.py的ajax调用,然后在html标记中显示输出。脚本run.py运行超过2分钟。但是在下面的js脚本中。一旦脚本开始运行。输出(输出的初始行)将显示在html标记中。脚本的其余部分将不会显示

$(document).ready(function(){   

$(sub).click(function(){ 
    alert("connecting to host")
            $.ajax({
                type:'POST',
                url:'/cgi-bin/run.py', 
                dataType: 'html',                                        
                success:function (z) {
                     $('#output').html(z);



                }


        });

        }) ;
});
我想知道ajax中是否有函数等待脚本完成(不仅仅是脚本的执行,而是等待结束),然后将整个输出显示到html标记

以下是我的python脚本:

import sys, os
import cgi, cgitb

    def __init__(self, address, username, password):
    # connects to host


    def sendShell(self, command):
     #opens shell

    def process(self):

                while self.shell.recv_ready():
                    info += self.shell.recv(1024)
                output = str(info, "utf8")
                print(output)

hostname = "z.com"
password = "yyy"
username = "dd"   
connection = ssh(hostname, username, password)
connection.openShell()
connection.sendShell("date");

jQuery.ajax()
有一个选项
async:false
,但是我建议不要这样做,只需在ajax回调中执行您需要的任何操作即可。同样,它被贬低了。

< P>将$Ajax函数的异步标志改为false几乎是正确的,但在这种情况下,脚本需要长时间运行,所以您需要考虑对这种请求使用长轮询。 原因是浏览器有ajax调用的最大超时时间,通常根据浏览器设置为1分钟(因此在您的情况下,1分钟后客户端/浏览器会停止连接并需要响应,但您希望它等到完成后再发送响应)。 因此,要克服这个问题,您必须每隔20秒或max timeout向py脚本发送一次另一个请求,以检查其是否完成

javascript端的代码片段:

function isDone(timestamp) {
    timestamp = timestamp || null;

    $.ajax({
        type:'POST',
        url:'/cgi-bin/run.py', 
        dataType: 'json',                                       
        data: { "timestamp": timestamp },
        timeout: 20000, // Or to whatever the max-timeout is, haven't checked that with jQuery.
        success: function (response) {
            if (response.done === false) {

                isDone(Date.now ());
            } else {
                // got the results, can continue.
                $('#output').html(response.output);
            }
        }
    });
}

isDone();
{
   "done": true, // or false if the script timed out.
   "output": html, // the variable that should contain the output when the py script is done, if its not done just send a null or don't send it back to the client at all.
   "timestamp": time.time() // if im not wrong that's how you get timestamp in py

}
Dynamcally or under setting, configure python script execution time to max or rather to 3 min, sense you mentioned it takes 2 min.

if (timestamp === null) { // first ajax call to this script.
    - Start long processing task.
    - Write the output to a file.
}

do { //  check this code block every 1 sec
    if (the file you are writing to was created and is complete / unlocked) {
        - Read from that file the content, save it into a variable and delete the file from your system.
        - Output the above mentioned response with done set to true and the current timestamp.
        - Break outside of the loop
    }

    if (timed out) { // basically check if 20 second passed sense starting timestamp.
        - Output the above mentioned response with done set to false and the current timestamp.
        - Break outside of the loop.
    }

    sleep for 1 sec, you don't want to kill your CPU usage.
} while (true)
我不确定pyton脚本应该是什么样子,如果您愿意,可以与我共享,我将尝试完成服务器端。 基本上,您应该做的是将脚本超时设置为最大值,并向客户端返回正确的响应

JSON响应应该如下所示:

function isDone(timestamp) {
    timestamp = timestamp || null;

    $.ajax({
        type:'POST',
        url:'/cgi-bin/run.py', 
        dataType: 'json',                                       
        data: { "timestamp": timestamp },
        timeout: 20000, // Or to whatever the max-timeout is, haven't checked that with jQuery.
        success: function (response) {
            if (response.done === false) {

                isDone(Date.now ());
            } else {
                // got the results, can continue.
                $('#output').html(response.output);
            }
        }
    });
}

isDone();
{
   "done": true, // or false if the script timed out.
   "output": html, // the variable that should contain the output when the py script is done, if its not done just send a null or don't send it back to the client at all.
   "timestamp": time.time() // if im not wrong that's how you get timestamp in py

}
Dynamcally or under setting, configure python script execution time to max or rather to 3 min, sense you mentioned it takes 2 min.

if (timestamp === null) { // first ajax call to this script.
    - Start long processing task.
    - Write the output to a file.
}

do { //  check this code block every 1 sec
    if (the file you are writing to was created and is complete / unlocked) {
        - Read from that file the content, save it into a variable and delete the file from your system.
        - Output the above mentioned response with done set to true and the current timestamp.
        - Break outside of the loop
    }

    if (timed out) { // basically check if 20 second passed sense starting timestamp.
        - Output the above mentioned response with done set to false and the current timestamp.
        - Break outside of the loop.
    }

    sleep for 1 sec, you don't want to kill your CPU usage.
} while (true)
伪代码中的服务器端:

function isDone(timestamp) {
    timestamp = timestamp || null;

    $.ajax({
        type:'POST',
        url:'/cgi-bin/run.py', 
        dataType: 'json',                                       
        data: { "timestamp": timestamp },
        timeout: 20000, // Or to whatever the max-timeout is, haven't checked that with jQuery.
        success: function (response) {
            if (response.done === false) {

                isDone(Date.now ());
            } else {
                // got the results, can continue.
                $('#output').html(response.output);
            }
        }
    });
}

isDone();
{
   "done": true, // or false if the script timed out.
   "output": html, // the variable that should contain the output when the py script is done, if its not done just send a null or don't send it back to the client at all.
   "timestamp": time.time() // if im not wrong that's how you get timestamp in py

}
Dynamcally or under setting, configure python script execution time to max or rather to 3 min, sense you mentioned it takes 2 min.

if (timestamp === null) { // first ajax call to this script.
    - Start long processing task.
    - Write the output to a file.
}

do { //  check this code block every 1 sec
    if (the file you are writing to was created and is complete / unlocked) {
        - Read from that file the content, save it into a variable and delete the file from your system.
        - Output the above mentioned response with done set to true and the current timestamp.
        - Break outside of the loop
    }

    if (timed out) { // basically check if 20 second passed sense starting timestamp.
        - Output the above mentioned response with done set to false and the current timestamp.
        - Break outside of the loop.
    }

    sleep for 1 sec, you don't want to kill your CPU usage.
} while (true)

import sys,os import cgi,cgib def_uuuinit_uu(self,地址,用户名,密码):#连接到主机def sendShell(self,命令):#打开shell def进程(self):而self.shell.recv_ready():alldata+=self.shell.recv(1024)strdata=str(alldata,“utf8”)打印(strdata)hostname=“z.com”password=“yyy”username=“dd”connection=ssh(主机名、用户名、密码)connection.openShell()connection.sendShell(“日期”);我已经在我的帖子中添加了这个片段@伊甸园感谢您的宝贵意见。在我的代码中,我应该在哪里添加代码片段的json部分?我不太熟悉py语法。在伪代码中应该是这样的..编辑我的答案OK@Rithesh我已经更新了我的答案。希望这能给你一个基本的想法。。