0

First ajax

function testAjax() {
  $.ajax({
    url: "getvalue.php",  
    success: function(data) {

      callback(data);
      //return data; 
    }
  });
}

Callback function

function callback(data) {
  return data;
}

Another Ajax that uses data from callback that is from another ajax

$.ajax({
   url: "getvalue.php",  
   success: function(data) {

     //how to get the data from callback function to be used here
     //return data; 
   }
});

I have an ajax call from a function and to get the data from that ajax request i have a callback function.

My question is how can I use the data from the callback function to be used in my second ajax request?

Giant
  • 1,619
  • 7
  • 33
  • 67
  • Try another ajax inside callback function.. – Geee Jun 02 '17 at 06:27
  • Put another AJAX in the success callback of the first one. – void Jun 02 '17 at 06:36
  • @void i may have to have multiple ajax request inside ajax request. i mean i will have like 3 levels of ajax inside. is there a cons for that? – Giant Jun 02 '17 at 06:39
  • @void i am just worried that there will drawback if i do this. can you explain if there will be drawback. i am looking for drawback of this solution as of this moment – Giant Jun 02 '17 at 06:40
  • @Giant the only drawback I can think of is that the requests will in synchronous manners and thus will need more time to finish. Otherwise I dont think three level of AJAX request will be of any trouble. – void Jun 02 '17 at 06:44
  • i see will you provide it as answer i would like to accept it as answer due to explanation and it claried my doubts @void – Giant Jun 02 '17 at 06:46

1 Answers1

1

Your ajax is asynchronous so your function returns null while the request is still running.
Try to use ajax like this

function testAjax() {
    $.ajax({
        url: "getvalue.php",
        success: function(data) {
            anotherAjax(data)
        }
    });
}

function anotherAjax(another_data) {
    $.ajax({
        url: "getvalue.php",  
        data: another_data,
        success: function(data) {
            // do something
        }
    });
}

testAjax();
UfguFugullu
  • 2,107
  • 13
  • 18