简体   繁体   中英

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

and here is AJAX1.jsp-- AJAX1.jsp

The first function - loadXMLDoc() checks from the Database about username availability using AJAX.jsp and returns the message string accordingly.

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 .

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

var xmlhttp;

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

Instead what i had done is declaring a new variable for the second function XMLDoc1()--

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

and change the variable name accordingly wherever applicable.

使用Charles调试请求和响应

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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