开发者

Need to stop execution of javascript method until cgi response is received

I am working on a 开发者_StackOverflow社区web app in which I am calling cgi-bin from backhand.I use ajax request for that.Now I want one scenario in which I want to wait till the cgi response is arrived.Actually one form action is called which is also a cgi. now I am able to do the task but method returns first and cgi response is received later.Depending on cgi response action needs to be called. Is it possible ? Please help... Thanks in advance... Here is the code I am using :-

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;

Now I want to call form action only when the flag true,calling this function in button onclick event with type = "submit".


keep the first response in a global variable, and for the second call, if it's dependent on the first one, just wait...

As I hate cross browser problems, here is the jQuery version:

$("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

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜