简体   繁体   English

javascript内的ajax调用

[英]ajax call inside javascript

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. 上面是我的代码,其中第一个ajax将检索给定的ip是否冲突。 But I face a problem that the ajax is responding slow and javascript is complete. 但是我面临一个问题,即ajax响应缓慢且javascript已完成。 So I am not able to complete my action so how to run first ajax and then javascript execution will continue? 因此,我无法完成我的操作,因此如何先运行Ajax,然后继续执行JavaScript?

Your method clearly indicates it's asynchronous 您的方法清楚地表明它是异步的

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

The last parameter (true) is the one! 最后一个参数(true)是那个!

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

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

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