Jump to content
Sign in to follow this  
Shinori

Ajax inside of Ajax?

Recommended Posts

Hello potential helpers,

im currently having issues with firing an ajax request inside of another ajax request.

Heres my code: 

Im not getting "alert(content)" :(

Condition if(shop[1] == "gserver") and if(name.includes(shopname) == true) are definetly true.

The function is correctly called as well.

No Im not using jquery for reasons.

Anyone has an idea why the 2nd requests isnt fired? Is it not waiting for the first one to finish?

function initiate_shop_game(shopname)
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
      if(this.readyState == 4 && this.status == 200)
      {
        var obj = this.responseText;
        var array = eval("[" + obj + "]");
        var r = 1;
        for(var i = 0; i < array.length; i++)
        {
          for(var x = 0; x < array[i].length; x++)
          {
            var shop = array[i][x] + '';
            shop = shop.split(",");
            if(shop[1] == "gserver")
            {
              var name = shop[2];
              var imgname = shop[7]
              var shopvalues = shop[3].split("{%TEND%}");
              var minslots = parseInt(shopvalues[1]);
              var maxslots = parseInt(shopvalues[2]);
              var minram= parseInt(shopvalues[3]);
              var maxram = parseInt(shopvalues[4]);
              var pricea = (parseFloat(shop[5]) / 30);
              var priceb = (parseFloat(shop[6] / 30));
              var content = [name,imgname,minslots,maxslots,minram,maxram,pricea,priceb];
              if(name.includes(shopname) == true)
              {
                var div = document.createElement('div');
                div.id = name;
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function()
                {
                  if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
                  {
                    alert(content);
                  };
                  xmlhttp.open('POST',"/template_shop.php", true);
                  xmlhttp.send();
                }
              }
            }
          }
        }
      }

    };
    xmlhttp.open('POST',"/get_shop_data.php", true);
    xmlhttp.send();
}

 

Share this post


Link to post
Share on other sites

You've put the open() and send() methods of the second request inside of the onreadystatechange handler.

  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×