繁体   English   中英

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

[英]How to resolve the conflict two ajax request at a same time

我有一个nominal.jsp页面,其中包含header.jsp。在这里,我第一次使用Ajax,用于header.jsp中的请求,然后第二次将Ajax请求调用到名义.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>   

再次感谢您的推进。

使用单个回调和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;
    }        
  }

参考文献

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM