0

NEW QUESTION. Read the edit for reference of the previous question.

I have this scenario. In the first ajax, I will get an ID from the database and then in the second ajax, I will use this ID.

$(document).ready(function(){  

  var y = null; // global variable, i guess
  function setID(x){
    y = x;
  }

  // Getting the ID
  $.ajax({    
    type: "GET",
    url: BASE_URL+'get/new_id',             
    dataType: "html",       
    success: function(response){                    
      setID(response); 
  }); 

  // Using the new ID
  $.ajax({    
    type: "POST",
    url: BASE_URL+'save/new_id',             
    dataType: "html", 
    data: {id: y}; // using the value from the global variable 
    // saving ID along with other stuff    
    success: function(response){                    
     console.log("You have successfully used the new ID"); 
  });      

});

This is still my current progress and I haven't finished reading this question. I'm still finding out how to implement the chosen answer there to my question. This is the final state of my question.

Community
  • 1
  • 1
Dev
  • 1,592
  • 2
  • 22
  • 45
  • Cleaning up dummy code. – Dev Jan 26 '16 at 17:43
  • @showdev: I think you've missed the point of what the linked question asks/answers. Given the code in this question, this is *exactly* a duplicate. The OP is trying to use the value obtained from an asynchronous operation before that value has been obtained. – David Jan 26 '16 at 17:45
  • @David Oh, you're right. Just saw the edited question. Thanks. – showdev Jan 26 '16 at 17:46
  • I have fixed my question after my first comment. I've read the linked question. I understand that the nature of the question is the same with this (just different situation, I guess). And I still don't quite understand the answers there. – Dev Jan 26 '16 at 17:58
  • 1
    You're defining the `hidden_id` variable before your `success` handler is executed. Whatever code you need to execute with the new input value could be called as a function from the success handler. Is there something specific in the linked answer(s) that you don't understand? – showdev Jan 26 '16 at 18:02
  • `Whatever code you need to execute with the new input value could be called as a function from the success handler.` I think I got it. The problem now will only be the implementation. I'll give it a try. Thank you. – Dev Jan 26 '16 at 18:11
  • Here's an example [using a callback function](https://jsfiddle.net/sj30zmu6/). – showdev Jan 26 '16 at 18:18
  • The linked answer portion you need is PROMISES for this; not the callback; this is exactly what the promise is for; – Mark Schultheiss Jan 26 '16 at 21:03

0 Answers0