Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何同时解决两个ajax请求之间的冲突_Javascript_Ajax_Servlets - Fatal编程技术网

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

Javascript 如何同时解决两个ajax请求之间的冲突,javascript,ajax,servlets,Javascript,Ajax,Servlets,我有一个nominal.jsp页面,其中包括header.jsp。在这里,我第一次为header.jsp中的请求使用Ajax,然后第二次为nominal.jsp调用Ajax请求,我在Ajax请求中面临冲突问题。由于此问题,我的首选下拉列表不会显示。有时,当在JavaScript中输入响应时,会显示下拉列表,如果响应未在JavaScript中输入,则不会显示下拉列表。尽我最大努力解决问题,但无法解决。请帮帮我,伙计们 谢谢你, My header.jsp代码: <script> hea

我有一个nominal.jsp页面,其中包括header.jsp。在这里,我第一次为header.jsp中的请求使用Ajax,然后第二次为nominal.jsp调用Ajax请求,我在Ajax请求中面临冲突问题。由于此问题,我的首选下拉列表不会显示。有时,当在JavaScript中输入响应时,会显示下拉列表,如果响应未在JavaScript中输入,则不会显示下拉列表。尽我最大努力解决问题,但无法解决。请帮帮我,伙计们

谢谢你,

My 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>   

头部显示();
函数头显示()
{var url='/summary?operation=header';
交易请求(url);
}  
函数transactionRequest(url)
{
if(window.XMLHttpRequest)
{              
req=新的XMLHttpRequest();
req.onreadystatechange=事务响应;
尝试
{ 
req.open(“POST”,url,true);//得到
}
捕获(e)
{
警报(“与服务器通信出现问题”\n“+e);
}                  
请求发送(空);
}       
else if(window.ActiveXObject)
{ 
//即
req=新的ActiveXObject(“Microsoft.XMLHTTP”);
如果(请求)
{ 
req.onreadystatechange=事务响应;
请求打开(“POST”,url,true);
请求发送();
}
}  
}
函数transactionResponse()
{         
如果(req.readyState==4)//完成
{
if(req.status==200)//正常响应
{var servletVal=req.responseText;
var myObject=eval(“(“+servletVal+”)”);
var userId=myObject.userId;
}}}......
这是我的nono.jsp代码:
函数displayNominal()
{
document.getElementById(“ajaxLoading”).style.display=“block”;
var url='''+'/nominalList';
postRequest(url);
}
函数postRequest(url){
if(window.XMLHttpRequest){
req=新的XMLHttpRequest();
req.onreadystatechange=nominalSelect;
试一试{
req.open(“POST”,url,true);//得到
}捕获(e){
警报(“与服务器通信出现问题”\n“+e);
}
请求发送(空);
}else if(window.ActiveXObject){
//即
req=新的ActiveXObject(“Microsoft.XMLHTTP”);
如果(请求){
req.onreadystatechange=nominalSelect;
请求打开(“POST”,url,true);
请求发送();
}
}
}
函数nominalSelect(){
如果(req.readyState==4)//完成
{
if(req.status==200)//正常响应
{
var servletVal=req.responseText;
var myObject=eval(“(“+servletVal+”)”);
var userId=myObject.userId;
if(userId==null | | userId==“”){
window.location='/accounts1/?status=session';
}
}}..
accounts1.co演示版-永远免费使用

再次感谢您的帮助。

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


@Ramesh:显然他不是?否则问题会被加上标签。不要把
req
作为一个全局变量!回到我自己做Ajax的时候,我会为每种类型的请求创建几个不同的变量。现在我会回答:使用JQuery来做Ajax调用,而不是自己做,你就不会有这个问题了。嗨bergi和developerwjk,非常感谢我完成了这个问题,再次非常感谢。