Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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_Infinite Loop - Fatal编程技术网

Javascript 为什么我的代码在使用AJAX时会进入无限循环?

Javascript 为什么我的代码在使用AJAX时会进入无限循环?,javascript,ajax,infinite-loop,Javascript,Ajax,Infinite Loop,我正在使用这两个函数尝试将客户和订单添加到我的数据库中。我遇到的问题是,我想执行addcustomer,然后执行addOrder,但当我这样尝试时,它进入了一个无限循环 function addCustomer(){ customer = localStorage.getItem('customer'); jsonCustomer = JSON.parse(customer); var xmlhttp = new XMLHttpRequest(); if(xmlhttp){

我正在使用这两个函数尝试将客户和订单添加到我的数据库中。我遇到的问题是,我想执行addcustomer,然后执行addOrder,但当我这样尝试时,它进入了一个无限循环

function addCustomer(){
  customer = localStorage.getItem('customer');
  jsonCustomer = JSON.parse(customer);

  var xmlhttp = new XMLHttpRequest();

  if(xmlhttp){
    xmlhttp.onreadystatechange = function(){
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
        response = JSON.parse(xmlhttp.responseText);

        customerID = response[0].ID;
        customerFirstName = response[0].FirstName;
        customerSurname = response[0].LastName;
        customerEmail = response[0].Email;
        customerPhoneNumber = response[0].PhoneNumber;
        customerAddress = response[0].Address;
        customerTowm = response[0].Town;
        customerCounty = response[0].County;
        customerPostCode = response[0].PostCode;

        details = "<p>ID: " + customerID + "</p>" + "<p>First Name: " + customerFirstName + "</p>" + "<p>Surname: " + customerSurname + "</p>" + "<p>Email: " + customerEmail + "</p>" + "<p>Phone Number: " + customerPhoneNumber + "</p>" + "<p>Address: " + customerAddress + "</p>" +
                  "<p>Town: " +  + "</p>" + "<p>County: " + customerCounty + "</p>" + "<p>Post Code: " + customerPostCode + "</p>";
        Alert.render("Customer Added", details);

      }
    }
  var url = "addCustomer.php?firstName=" + jsonCustomer.FirstName + "&surname=" + jsonCustomer.Surname + "&email=" + jsonCustomer.Email + "&phoneNumber=" + jsonCustomer.PhoneNumber + "&address=" + jsonCustomer.Address + "&town=" + jsonCustomer.Town + "&county=" + jsonCustomer.County + "&postCode=" + jsonCustomer.PostCode;
  xmlhttp.open("GET", url, false);
  xmlhttp.send();

  addOrder(customerID);

  }
}


function addOrder(customerID){
  customerID = addCustomer();
  var xmlhttp = new XMLHttpRequest();


  if(xmlhttp){
    xmlhttp.onreadystatechange = function(){
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
        console.log(xmlhttp.responseText);

        response = JSON.parse(xmlhttp.responseText);

        orderID = response[0].ID;
        orderDate = response[0].Date;
        orderPrice = response[0].TotalPrice;
        orderCustomerID = response[0].Customer_ID;


        details = "<p>ID: " + orderID + "</p>" + "<p>Date: " + orderDate + "</p>" + "<p>Total Price: " + orderPrice + "</p>" + "<p>Customer ID: " + orderCustomerID + "</p>";
        Alert.render("Customer Added", details);


      }
    }

    var url = "../Administration/Checkout/addOrder.php?customerID=" + customerID + "&totalPrice=" + totalPrice;
    xmlhttp.open("GET", url, false);
    xmlhttp.send();

  }
}
函数addCustomer(){
customer=localStorage.getItem('customer');
jsonCustomer=JSON.parse(customer);
var xmlhttp=new XMLHttpRequest();
if(xmlhttp){
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
response=JSON.parse(xmlhttp.responseText);
customerID=响应[0]。ID;
customerFirstName=响应[0]。名字;
customerSurname=响应[0]。姓氏;
customerEmail=响应[0]。电子邮件;
customerPhoneNumber=响应[0]。电话号码;
customerAddress=响应[0]。地址;
customerTowm=响应[0]。城镇;
customerCounty=响应[0]。县;
customerPostCode=响应[0]。邮政编码;
详细信息=“ID:”+customerID+”

“+”名字:“+customerFirstName+”

“+”姓氏:“+customerurname+”

电子邮件:“+customerEmail+”

“+”电话号码:“+customerPhoneNumber+”

“+”地址:“+customerAddress+”

”+ “镇:”++“

县:“+customerCounty+”

“+”邮编:“+customerPostCode+”

”; 警报。呈现(“客户添加”,详细信息); } } var url=“addCustomer.php?firstName=“+jsonCustomer.firstName+”&姓氏=“+jsonCustomer.姓氏+”&电子邮件=“+jsonCustomer.email+”&电话号码=“+jsonCustomer.phoneNumber+”&地址=“+jsonCustomer.address+”&城镇=“+jsonCustomer.town+”&县=“+jsonCustomer.country+”&邮政编码=“+jsonCustomer.postCode; open(“GET”,url,false); xmlhttp.send(); addOrder(customerID); } } 函数addOrder(customerID){ customerID=addCustomer(); var xmlhttp=new XMLHttpRequest(); if(xmlhttp){ xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ log(xmlhttp.responseText); response=JSON.parse(xmlhttp.responseText); orderID=响应[0]。ID; orderDate=响应[0]。日期; orderPrice=响应[0]。总价; orderCustomerID=响应[0]。客户ID; 详细信息=“ID:”+orderID+”

“+”日期:“+orderDate+”

“+”总价:“+orderPrice+”

“+”客户ID:“+orderCustomerID+”

”; 警报。呈现(“客户添加”,详细信息); } } var url=“../Administration/Checkout/addOrder.php?customerID=“+customerID+”&totalPrice=“+totalPrice; open(“GET”,url,false); xmlhttp.send(); } }
  • addCustomer
    将始终在
    addCustomer
    结束前调用
    addOrder
    (除非浏览器不支持
    XMLHttpRequest
  • addOrder
    将始终调用
    addCustomer
    (在其第一行)

您需要一个条件来阻止其中一个调用的发生,以避免无限循环。

您有
addCustomer()
调用
addOrder
,它在第一行再次调用
addCustomer
,这就是无限调用的原因

function addCustomer(){
  customer = localStorage.getItem('customer');
  jsonCustomer = JSON.parse(customer);

  var xmlhttp = new XMLHttpRequest();

  if(xmlhttp){
    xmlhttp.onreadystatechange = function(){
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
        response = JSON.parse(xmlhttp.responseText);

        customerID = response[0].ID;
        customerFirstName = response[0].FirstName;
        customerSurname = response[0].LastName;
        customerEmail = response[0].Email;
        customerPhoneNumber = response[0].PhoneNumber;
        customerAddress = response[0].Address;
        customerTowm = response[0].Town;
        customerCounty = response[0].County;
        customerPostCode = response[0].PostCode;

        details = "<p>ID: " + customerID + "</p>" + "<p>First Name: " + customerFirstName + "</p>" + "<p>Surname: " + customerSurname + "</p>" + "<p>Email: " + customerEmail + "</p>" + "<p>Phone Number: " + customerPhoneNumber + "</p>" + "<p>Address: " + customerAddress + "</p>" +
                  "<p>Town: " +  + "</p>" + "<p>County: " + customerCounty + "</p>" + "<p>Post Code: " + customerPostCode + "</p>";
        Alert.render("Customer Added", details);

      }
    }
  var url = "addCustomer.php?firstName=" + jsonCustomer.FirstName + "&surname=" + jsonCustomer.Surname + "&email=" + jsonCustomer.Email + "&phoneNumber=" + jsonCustomer.PhoneNumber + "&address=" + jsonCustomer.Address + "&town=" + jsonCustomer.Town + "&county=" + jsonCustomer.County + "&postCode=" + jsonCustomer.PostCode;
  xmlhttp.open("GET", url, false);
  xmlhttp.send();

  addOrder(customerID);

  }
}


function addOrder(customerID){
  customerID = addCustomer();
  var xmlhttp = new XMLHttpRequest();


  if(xmlhttp){
    xmlhttp.onreadystatechange = function(){
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
        console.log(xmlhttp.responseText);

        response = JSON.parse(xmlhttp.responseText);

        orderID = response[0].ID;
        orderDate = response[0].Date;
        orderPrice = response[0].TotalPrice;
        orderCustomerID = response[0].Customer_ID;


        details = "<p>ID: " + orderID + "</p>" + "<p>Date: " + orderDate + "</p>" + "<p>Total Price: " + orderPrice + "</p>" + "<p>Customer ID: " + orderCustomerID + "</p>";
        Alert.render("Customer Added", details);


      }
    }

    var url = "../Administration/Checkout/addOrder.php?customerID=" + customerID + "&totalPrice=" + totalPrice;
    xmlhttp.open("GET", url, false);
    xmlhttp.send();

  }
}
由于
addOrder
方法需要
addCustomer
ajax调用返回的
customerId
,因此您需要在
addCustomer
的成功中调用
addOrder
方法,并可以将
customerId
作为pram传递给
addOrder
方法

function addCustomer() {
    customer = localStorage.getItem('customer');
    jsonCustomer = JSON.parse(customer);

    var xmlhttp = new XMLHttpRequest();

    if (xmlhttp) {
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var response = JSON.parse(xmlhttp.responseText);

                customerID = response[0].ID;
                customerFirstName = response[0].FirstName;
                customerSurname = response[0].LastName;
                customerEmail = response[0].Email;
                customerPhoneNumber = response[0].PhoneNumber;
                customerAddress = response[0].Address;
                customerTowm = response[0].Town;
                customerCounty = response[0].County;
                customerPostCode = response[0].PostCode;

                details = "<p>ID: " + customerID + "</p>" + "<p>First Name: " + customerFirstName + "</p>" + "<p>Surname: " + customerSurname + "</p>" + "<p>Email: " + customerEmail + "</p>" + "<p>Phone Number: " + customerPhoneNumber + "</p>" + "<p>Address: " + customerAddress + "</p>" +
                    "<p>Town: " + +"</p>" + "<p>County: " + customerCounty + "</p>" + "<p>Post Code: " + customerPostCode + "</p>";
                Alert.render("Customer Added", details);

                //need to call it after the addCustomer is finished
                addOrder(customerID);

            }
        }
        var url = "addCustomer.php?firstName=" + jsonCustomer.FirstName + "&surname=" + jsonCustomer.Surname + "&email=" + jsonCustomer.Email + "&phoneNumber=" + jsonCustomer.PhoneNumber + "&address=" + jsonCustomer.Address + "&town=" + jsonCustomer.Town + "&county=" + jsonCustomer.County + "&postCode=" + jsonCustomer.PostCode;
        xmlhttp.open("GET", url, false);
        xmlhttp.send();
    }
}


function addOrder(customerID) {
    var xmlhttp = new XMLHttpRequest();


    if (xmlhttp) {
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                console.log(xmlhttp.responseText);

                response = JSON.parse(xmlhttp.responseText);

                orderID = response[0].ID;
                orderDate = response[0].Date;
                orderPrice = response[0].TotalPrice;
                orderCustomerID = response[0].Customer_ID;


                details = "<p>ID: " + orderID + "</p>" + "<p>Date: " + orderDate + "</p>" + "<p>Total Price: " + orderPrice + "</p>" + "<p>Customer ID: " + orderCustomerID + "</p>";
                Alert.render("Customer Added", details);


            }
        }

        var url = "../Administration/Checkout/addOrder.php?customerID=" + customerID + "&totalPrice=" + totalPrice;
        xmlhttp.open("GET", url, false);
        xmlhttp.send();

    }
}
函数addCustomer(){
customer=localStorage.getItem('customer');
jsonCustomer=JSON.parse(customer);
var xmlhttp=new XMLHttpRequest();
if(xmlhttp){
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var response=JSON.parse(xmlhttp.responseText);
customerID=响应[0]。ID;
customerFirstName=响应[0]。名字;
customerSurname=响应[0]。姓氏;
customerEmail=响应[0]。电子邮件;
customerPhoneNumber=响应[0]。电话号码;
customerAddress=响应[0]。地址;
customerTowm=响应[0]。城镇;
customerCounty=响应[0]。县;
customerPostCode=响应[0]。邮政编码;
详细信息=“ID:”+customerID+”

“+”名字:“+customerFirstName+”

“+”姓氏:“+customerurname+”

电子邮件:“+customerEmail+”

“+”电话号码:“+customerPhoneNumber+”

“+”地址:“+customerAddress+”

”+ “镇:”++“

县:“+customerCounty+”

“+”邮编:“+customerPostCode+”

”; 警报。呈现(“客户添加”,详细信息); //需要在addCustomer完成后调用它 addOrder(customerID); } } var url=“addCustomer.php?firstName=“+jsonCustomer.firstName+”&姓氏=“+jsonCustomer.姓氏+”&电子邮件=“+jsonCustomer.email+”&电话号码=“+jsonCustomer.phoneNumber+”&地址=“+jsonCustomer.address+”&城镇=“+jsonCustomer.town+”&县=“+jsonCustomer.country+”&邮政编码=“+jsonCustomer.postCode; open(“GET”,url,false); xmlhttp.send(); } } 函数addOrder(customerID){ var xmlhttp=new XMLHttpRequest(); if(xmlhttp){ xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ log(xmlhttp.responseText); response=JSON.parse(xmlhttp.responseText); orderID=响应[0]。ID; orderDate=响应[0]。日期;