0

I have an AJAX response that will return an array and I push the data onto an array that I created. The array that I created is within the success parameter and I am trying to access that data so it can be used in another ajax call. The global array that it would be pushed to is one of 3 array's that I will be creating and joining to be used at a later time.

currently I have:

$.ajax({
  type: "GET",
  url: "https://api.twitch.tv/helix/streams?first=20",
  headers: {
    "Client-ID": "NOTHING TO HERE",
    "Accept": "application/vnd.twitchtv.v5+json"
  },
  success: function(data1) {
    var userID = data1.data;
    var userIDArr = [];

    for (var i = 0; i < userID.length; i++) {
      userIDArr.push(data1.data[i].user_id);
    }
    console.log(userIDArr);
  },
  dataType: "json"
});

You can see the success parameter has the arr that I would like to utilize outside the AJAX response and in another. Here:

$.ajax({
  type: "GET",
  url: "https://api.twitch.tv/kraken/users?login=" + userIDArr,
  headers: {
    "Client-ID": "NOTHING TO HERE",
    "Accept": "application/vnd.twitchtv.v5+json"
  },
  success: function(data) {}
});
  • the short answer is no. You're going to have to manipulate the response data from the server from within the success function. – JJJ Nov 06 '17 at 18:04
  • Yes it is possible if you define userIDArr outside the functions to make it global, it is however not recommended to turn off async but instead code the calls to take advantage of the async. Just execute the next function in the success or set a flag to tell the other function that the value is available – mplungjan Nov 06 '17 at 18:05
  • 1
    What exactly isn't working? Are you trying to make the second AJAX call before the first one finishes? Also, don't use `async: false`. I wouldn't be surprised if browsers just stop supporting it entirely. – David Nov 06 '17 at 18:05
  • https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – epascarello Nov 06 '17 at 18:06
  • I forgot to remove the async:false will do. Thanks. – Rembrandt Reyes Nov 06 '17 at 18:06
  • Also, if you want your variable to be global, did you try moving `var userIDArr = [];` to *global scope*? – David Nov 06 '17 at 18:06
  • I did move it outside of the scope, just moved it back in to work with console.log – Rembrandt Reyes Nov 06 '17 at 18:07
  • 1
    @RembrandtReyes: How is the second AJAX call invoked after the first one completes? There's no code in the first AJAX call doing that. If you put the second AJAX call in the callback for the first one then you don't even need a global variable. It'll be in the same scope already. – David Nov 06 '17 at 18:10
  • @David. So something along the lines of Ajax1({...success: function{var userIDArr[]; Ajax2({...success: function{userIDArr})}}); – Rembrandt Reyes Nov 06 '17 at 18:17
  • I have no idea what you are trying to do, but in theory, you could load a js script that will create the array in the global scope. see jQuery.getScript(). I am not saying that's a good idea, but it will work. – Paun Narcis Iulian Nov 07 '17 at 15:39

0 Answers0