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?

6
  • Try another ajax inside callback function.. Commented Jun 2, 2017 at 6:27
  • Put another AJAX in the success callback of the first one. Commented Jun 2, 2017 at 6: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? Commented Jun 2, 2017 at 6: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 Commented Jun 2, 2017 at 6: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. Commented Jun 2, 2017 at 6:44

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.