简体   繁体   English

在一个函数中使用2个Ajax调用

[英]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. 我想做一个ajax函数来发布使用另一个ajax函数加载的数据。 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? 这是我应该考虑的解决方案,还是有其他方法可以等待第一个ajax调用完成?

gr 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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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