简体   繁体   中英

using 2 ajax calls in one function

I want to make a ajax function to post data that I've loaded using another ajax function. I already discovered to use a callback function, but how do I pass data from the one function to the other. I've tried this :

getData(function{
 $.ajax({
  do stuff...,
  data: dataArray,
  do stuff...
 )}
});

function getData(callback){
 $.ajax({
  do stuff...,
  data: dataArray,
  do stuff...,
  success: function(data){
   dataArray = fill array with stuff;
   callback();
  }
 )}
};

looks like the array isn't available to the callback function...

Is this the solution I should think of, or is there another way to wait for the first ajax call to finish?

gr

Your code seems so complicated so I am writing a new one. You can make your own by looking at my code.

Try this;

$.ajax({
type: 'get',
url: 'http://geturl.com',
success: postdata(dataToPost)});

function postdata(dataToPost){
$.ajax({
type:'post',
url: 'http://posturl.com',
data: datatoPost
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM