简体   繁体   English

处理页面上的多个AJAX请求时出错

[英]ERROR in handling Multiple AJAX request on a page

<script type="text/javascript">
    function loadXMLDoc() {
        var xmlhttp;
        var k = document.getElementById("usernamesignup").value;
        var urls = "AJAX.jsp?ver=" + k;

        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                //document.getElementById("err").style.color="red";
                document.getElementById("err").innerHTML = xmlhttp.responseText;

            }
        }
        xmlhttp.open("GET", urls, true);
        xmlhttp.send();
    }

    function loadXMLDoc1() {
        var xmlhttp;
        var k1 = document.getElementById("emailsignup").value;
        var urls1 = "AJAX1.jsp?ver1=" + k1;

        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                //document.getElementById("err1").style.color="red";
                document.getElementById("err1").innerHTML = xmlhttp.responseText;

            }
        }
        xmlhttp.open("GET", urls1, true);
        xmlhttp.send();
    }
</script>

here is AJAX.jsp-- AJAX.jsp 这是AJAX.jsp-- AJAX.jsp

and here is AJAX1.jsp-- AJAX1.jsp 这是AJAX1.jsp- AJAX1.jsp

The first function - loadXMLDoc() checks from the Database about username availability using AJAX.jsp and returns the message string accordingly. 第一个函数AJAX.jsp loadXMLDoc()使用AJAX.jsp从数据库检查用户名是否可用,并相应地返回消息字符串。

The second function - loadXMLDoc1() intends to do the same thing with the email, but doesn't return any messages as it does while checking the USERNAME . 第二个功能loadXMLDoc1()打算对电子邮件执行相同的操作,但是不返回检查USERNAME时返回的任何消息。

Is there a problem with the code??? 代码有问题吗??? Whats the solution.... Thanks 解决方案是什么...。谢谢

THANKS GUYS FOR YOUR VIEWS AND RESPONSES, but this is where i was missing out...i was declaring the same XMLHttpRequest variable for the two requests... 感谢您的意见和建议,但这是我所错过的地方...我为两个请求声明了相同的XMLHttpRequest变量...

var xmlhttp;

if (window.XMLHttpRequest)
 {
   xmlhttp=new XMLHttpRequest();
 }

Instead what i had done is declaring a new variable for the second function XMLDoc1()-- 相反,我所做的是为第二个函数XMLDoc1()声明了一个新变量-

var xmlhttp1;
if (window.XMLHttpRequest)
{
    xmlhttp1=new XMLHttpRequest();
}

and change the variable name accordingly wherever applicable. 并在适用的情况下相应地更改变量名称。

使用Charles调试请求和响应

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

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