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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...