0

我正在开发一个 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 事件中调用此函数。

4

1 回答 1

0

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

因为我讨厌跨浏览器问题,这里是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

});
于 2011-09-09T08:08:31.957 回答