weixin_33726313 2015-09-15 13:48 采纳率: 0%
浏览 37

JavaScript内的Ajax呼叫

var oldip = document.getElementById('ip').value;
var newip1 = document.getElementById('txtintip1').value;
var newip2 = document.getElementById('txtintip2').value;
var newip3 = document.getElementById('txtintip3').value;
var newip4 = document.getElementById('txtintip4').value;
var newip = newip1 + "." + newip2 + "." + newip3 + "." + newip4;
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          if (xmlhttp.responseText == "pinging") {
              alert("IP is already in use");
          }         
    }
}
xmlhttp.open("GET", "checkping.php?ip=" +newip, true);
xmlhttp.send();

if (document.getElementById('interface').value == "default") {
    alert("Select Interface");
    document.registerlist.interface.focus();
    return false;
} 
if (document.getElementById('txtintip1').value === "") {
    alert("Enter Valid IP Address");
    document.registerlist.txtintip1.focus();
    return false;
}

if (document.getElementById('txtintip2').value === "") {
    alert("Enter Valid IP Address");
    document.registerlist.txtintip2.focus();
    return false;
}
if (document.getElementById('txtintip3').value === "") {
    alert("Enter Valid IP Address");
    document.registerlist.txtintip3.focus();
    return false;
}
if (document.getElementById('txtintip4').value === "") {
    alert("Enter Valid IP Address");
    document.registerlist.txtintip4.focus();
    return false;
}

Above is my code in which first ajax will retrieve whether given ip is conflicting or not. But I face a problem that the ajax is responding slow and javascript is complete. So I am not able to complete my action so how to run first ajax and then javascript execution will continue?

  • 写回答

1条回答 默认 最新

  • weixin_33736832 2015-09-15 14:15
    关注

    Your method clearly indicates it's asynchronous

    xmlhttp.open("GET", "checkping.php?ip=" +newip, true);
    

    The last parameter (true) is the one!

    http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

    评论

报告相同问题?