AJAX触发远程脚本然后检查成功响应脚本、AJAX

2023-09-10 21:59:00 作者:间歇性、失恋

我一直在寻找我的脑袋,但我似乎无法包住小的帮助,我觉得在我的脑海。

I've been searching my brains out but I can't seem to wrap my head around the little help I find.

我正在正被从另一个数据库馈送通过数据的数据库。该CSV传输由第三方服务器提供可执行流动,它编译和传递数据进行处理。

I'm running a database that is being fed by data from another DB. The csv transport is handled by a third party server providing executable "flows" which compile and deliver the data.

我有一个PHP脚本来处理请求(不能直接通过Javascript,因为缺少访问控制 - 允许 - 原产地标题的完成)。但是,这很好地运行。我可以触发流程。

I have a php script to handle the request (can't be done directly via Javascript because of the missing 'Access-Control-Allow-Origin' header). But this runs nicely. I can trigger the flow.

这是没有问题的,但。 我想要做的:触发按钮的流动@onClick像这样的东西:

This is not the problem though. What I want to do: trigger the flow @onClick of a button with something like this:

function trigger_func(flowID) {
    $.ajax({
        url: './ajaxPHP_handler.php',
        data: "flowid="+flowID,
        dataType: 'json',
        success: function(result) {
            var jsonResult  =   jQuery.parseJSON(result);
            console.log(jsonResult.runID);
        }
    });
}

随着flowID所得的runid我想回来看看像每一秒左右。

With the flowID and the resulting runID I want to check back like every second or so.

function check_status(flowID, runID) {
    $.ajax({
        url: './ajaxPHP_handler.php',
        data: "flowid="+flowID+"&action=status&runId="+runID,
        dataType: 'json',
        success: function(result){...}
    });
}

这将返回流的状态/进度。 这将开始几秒钟,状态== null,则继续到状态==运行,最后状态==成功。 我已经得到了check_status()的IE浏览器15倍带的setTimeout在trigger_func()的成功,函数内运行的循环和正常工作了。 但我不能为我的生活弄清楚如何我将连接这些东西在一起,把它检查,直到状态为成功,然后停止检查,更新网页内容等等...

This will return the status / progress of the flow. It will start for a few seconds with status==null, then go on to status=='running' and finally status=='success'. I have gotten check_status() to run for i.e. 15 times with a setTimeout in a for loop within the success-function of trigger_func() and it works fine too. But I cannot for the life of me figure out how I would link this stuff together to have it checking until status is 'success' and then stop checking, update page content and so on...

我还拨弄着像

trigger_func(id).done(function(result){
console.log(result);
});

这也工作,但我仍然不认为我的方式进一步的检查每一秒,直到成功。我想这可以归结为获取变量'状态'回到我的循环,所以我可以打破它。

This works too but still I can't think my way further to the checking every second until 'success'. I guess it comes down to getting the variable 'status' back into my loop so I can break it.

也许有人知道一个COM prehensible例如某处在线...

Maybe someone knows of a comprehensible example somewhere online...

推荐答案

你可以这样做:

function periodically_check_status_until_success(flowID, runID) {
    setTimeout(function() {
        $.ajax({
            url: './ajaxPHP_handler.php',
            data: { flowid: flowID, action: status, runId: runID },
            dataType: 'json',
            success: function(result){
                 if (result != 'success') {
                     periodically_check_status_until_success(flowID, runID);
                 }
            }
        });
    }, 5000); // Five seconds
}

请注意:您可以使用对象为数据选项,而不是自己将字符串

Note: You can use an object for the data option, rather than concatenate the string yourself.