如何同时解决冲突两个ajax请求

时间:2014-05-01 05:59:08

标签: javascript ajax servlets

我有一个nominal.jsp页面,其中包括header.jsp.Here我是第一次使用Ajax,对于header.jsp中的请求,然后第二次将Ajax请求调用到nominal.jsp,我正面临Ajax请求中的冲突问题。由于此问题,我的首选下拉列表不会显示。有时,当在JavaScript中输入响应时,会显示下拉列表,如果未在JavaScript中输入响应,则不会显示下拉列表。尽我所能来解决问题,但无法解决问题。请帮帮我们

谢谢你,

我的header.jsp代码:

<script>
headerDisplay();
function headerDisplay()
{   var url ='<%=request.getContextPath()%>/summary?operation=header';
    transactionRequest(url);    
}  
function transactionRequest(url)
{
        if (window.XMLHttpRequest) 
         {              
            req = new XMLHttpRequest();                   
            req.onreadystatechange = transactionResponse;                   
                     try
                     { 
                               req.open("POST", url, true); //was get                   
                           }
                    catch(e) 
                     {
                        alert("Problem Communicating with Server\n"+e);
                     }                  
              req.send(null);
        }       

         else if (window.ActiveXObject) 
        { 
             // IE
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req)
                { 

                    req.onreadystatechange = transactionResponse;  
                    req.open("POST", url, true);
                    req.send();
                }
        }  
}
function transactionResponse()
{         
      if (req.readyState == 4)  // Complete
       {
                 if (req.status == 200) // OK response

             {      var servletVal = req.responseText;      
                 var myObject = eval('(' + servletVal + ')');                      
                     var userId = myObject.userId;

}}}......

</script>


And,this is my nono.jsp code:


<%@include file="/pages/common/header.jsp"%>

<script>
function displayNominal()
{
    document.getElementById("ajaxLoading").style.display="block";
    var url ='<%=request.getContextPath()%>'+'/nominalList';
    postRequest(url);
}

function postRequest(url) {
        if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = nominalSelect;
        try {
            req.open("POST", url, true); //was get                  
        } catch (e) {
            alert("Problem Communicating with Server\n" + e);
        }
        req.send(null);
    } else if (window.ActiveXObject) {
        // IE
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = nominalSelect;
            req.open("POST", url, true);
            req.send();
        }
    }

}
function nominalSelect() {

    if (req.readyState == 4) // Complete
    {

        if (req.status == 200) // OK response
        {
            var servletVal = req.responseText;

            var myObject = eval('(' + servletVal + ')');
            var userId = myObject.userId;
            if (userId == null || userId == "") {
                window.location = '/accounts1/?status=session';
            }
}}..

</script>

<body class="bodystyle" onload="displayNominal()">
<% if("N".equals(roleDemoStatus))
                 {%>
<!-- /#demo Header -->
    <div style="top: 0px; display: block;" id="header" class="fixed">
        <div class="outer">
            <h1 class="blog-title" style="text-align:center;margin-top:10px;"><span style="font-weight: normal; color:#777777; font-size: 30px;">accounts<font color="#5DA915">1</font>.co</span> Demo Only - <a href="https://accounts1.co/accounts1/pages/userRegistration/signup1.jsp"><font color="red">Click Here</font></a> To Use For Free Forever</h1>
        </div><!-- .outer -->
        <div style="display: block;" class="shadow"></div>
    </div>
<!-- /#Demo Header -->
    <%}  %> 
</body>   

再次感谢您的进步。

1 个答案:

答案 0 :(得分:0)

使用单个回调和try/catch块来强制执行请求订单:

function transactionResponse()
  {
  // Check whether this is the initial callback or a subsequent one
  if (!!transactionResponse.state)
    {        
      try
        {
        //POST data from the GET request
        }
      catch(e)
        {
        //Get data from the GET request
        }
      }

  // Set state after the initial callback reference
  else
    {
    transactionResponse.state = this;
    }        
  }

<强>参考