简体   繁体   English

如何在调用下一个请求之前完成第一个 Ajax Get 请求?

[英]How can I make first Ajax Get request be finish before I call next one?

I have to methods, that return promises.我必须方法,即返回承诺。 And I want to make second method to be executed after first would get and manipulate data.我想让第二个方法在第一个获取和操作数据之后执行。 This question has being asked several times, still cannot find a way to make it work.这个问题已经被问了好几次了,仍然找不到让它工作的方法。 Here is my code:这是我的代码:

 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"));

And here is the result witch I get:这是我得到的结果女巫:

Starting Vedtak
Starting Spesialitet
Everything is done
Success Vedtak
Success Spesialitet

And the result I would like to get:我想得到的结果:

Starting Vedtak
Success Vedtak
Starting Spesialitet
Success Spesialitet
Everything is done

You can use success calls in order to chain your functions, like this:您可以使用success调用来链接您的函数,如下所示:

self.getVedtak().success(function(){ 
  self.getSpesialitet().success(function(){ 
    console.log('Everything is done'); 
  }) 
})

Working fiddle: https://jsfiddle.net/robertrozas/sjg44pz2/1/工作小提琴: https : //jsfiddle.net/robertrozas/sjg44pz2/1/

Please understand that $.get , $.post and $.ajax calls are always asynchronous calls.请理解$.get$.post$.ajax调用始终是异步调用。 It's like fire and forget.这就像火灾和忘记。 It doesn't wait until the execution is complete.它不会等到执行完成。 To make your code work, you need to call your second method when the first method has returned.为了使您的代码工作,您需要在第一个方法返回时调用第二个方法。

暂无
暂无

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

相关问题 生成 ajax 调用的循环数组,我需要在下一个调用开始之前完成第一个调用 - Looping array that generates ajax call, i need the first call to finish before next one starts 我怎样才能让这段代码等待第一个 function 完成,然后再执行第二个或 function,秒数更少? - How can I make this code wait for the first function to finish before executing the second one or the function with less seconds? 在我的车把模板完成加载之前,如何获得AJAX调用以未完成? - How can I get an AJAX call to NOT finish before my handlebar template is done loading? 在继续下一个之前,我如何等待这个 animation 完成? - How do I wait for this animation to finish before moving on to the next one? 如何确保 ajax/jquery 调用在 html 页面加载之前完成? - How can I make sure ajax/jquery calls finish before an html page loads? 在执行另一个函数之前,如何使jquery等待一个函数完成? - How can I make jquery wait for one function to finish before executing another function? 如何在AJAX请求中使用GET调用PHP文件? - How can I call a PHP file with GET on an AJAX request? 如何在一个ajax函数完成之前加载jquery函数 - how to make the jquery function load before one ajax function finish 如何创建一个Javascript函数调用列表,以等待上一个函数完成之后再调用下一个? - How do I create a Javascript function call list that waits for the previous function to finish before calling the next? 如何使循环等待db promise完成,然后再继续下一次迭代? - How do I make for loop wait for db promise to finish before it continues to next iteration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM