![](/img/trans.png)
[英]Looping array that generates ajax call, i need the first call to finish before next one starts
[英]How can I make first Ajax Get request be finish before I call next one?
我必须方法,即返回承诺。 我想让第二个方法在第一个获取和操作数据之后执行。 这个问题已经被问了好几次了,仍然找不到让它工作的方法。 这是我的代码:
self.getSpesialitet = function () { console.log("Starting Spesialitet"); return $.get(options.getSpesialitetUrl, { 'recno': recno }, function (data) { ////Code console.log("Success Spesialitet"); }); }; self.getVedtak = function () { console.log("Starting Vedtak"); return $.get(options.getVedtakUrl, { 'recno': recno }, function (data) { ////Code console.log("Success Vedtak"); }); }; $.when(self.getSpesialitet()).then(self.getVedtak()).then(console.log("Everything is done"));
这是我得到的结果女巫:
Starting Vedtak
Starting Spesialitet
Everything is done
Success Vedtak
Success Spesialitet
我想得到的结果:
Starting Vedtak
Success Vedtak
Starting Spesialitet
Success Spesialitet
Everything is done
您可以使用success
调用来链接您的函数,如下所示:
self.getVedtak().success(function(){
self.getSpesialitet().success(function(){
console.log('Everything is done');
})
})
请理解$.get
、 $.post
和$.ajax
调用始终是异步调用。 这就像火灾和忘记。 它不会等到执行完成。 为了使您的代码工作,您需要在第一个方法返回时调用第二个方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.