【问题标题】:Need to stop execution of javascript method until cgi response is received需要停止执行 javascript 方法,直到收到 cgi 响应
【发布时间】:2011-09-09 08:03:29
【问题描述】:

我正在开发一个 web 应用程序,在该应用程序中我从反手调用 cgi-bin。我为此使用 ajax 请求。现在我想要一个场景,我想等到 cgi 响应到达。实际上是一个表单操作被称为也是一个cgi。 现在我能够完成任务,但方法首先返回,稍后收到 cgi 响应。取决于 cgi 响应操作需要被调用。 是否可以 ? 请帮忙... 提前致谢... 这是我正在使用的代码:-

var flag = "";
if (xmlHttpReq.readyState == 0){        
        xmlHttpReq.open('POST', destinationURL, true);
        xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4) {                
                if(xmlHttpReq.responseText == "OK"){
                    flag = true;
                }
                else{        
                    flag = false;
                }
            }            
        }
        xmlHttpReq.send();

    }
    return flag;

现在我只想在标志为 true 时调用表单操作,在 type = "submit" 的按钮 onclick 事件中调用此函数。

【问题讨论】:

  • 可以同步进行ajax请求。这就是你所需要的吗?
  • 我正在这样做,但即使我没有得到预期的输出..

标签: javascript html forms cgi cgi-bin


【解决方案1】:

将第一个响应保存在全局变量中,对于第二个调用,如果它依赖于第一个响应,则等待...

由于我讨厌跨浏览器问题,这里是jQuery 版本:

$("form").submit(function() {

    // let's get what we need
    $.ajax({
        url: destinationURL,
        context: document.body,
        async: false, // you want a synchronous call
        success: function(){
          // done
        },
        error: function() { alert('something went wrong...'); }
    });

    // Continue your work, the line below will only be processed
    //   after the $.ajax call is made

});

【讨论】:

    猜你喜欢
    • 2014-04-18
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多