简体   繁体   English

Ajax等到Api完成后再执行下一个功能

[英]Ajax to wait until Api Is done before next function is executed

I'm a noob with this stuff(so please be patient), All i want to do is execute a function after the API is completely done, at the moment the JavaScript is posting and reading at the same time giving me unwanted results. 我是这个菜的菜鸟(所以请耐心等待),我要做的就是在API完全完成之后执行一个函数,此刻JavaScript正在同时发布和阅读,这给了我不想要的结果。

        function getData() {
            return $.ajax({ type: "POST", 
            data: JSON.stringify(test), 
            url:"/api/answers/", 
            contentType: "application/json" 

So I read this article jQuery ajax success callback function definition 所以我读了这篇文章jQuery ajax成功回调函数定义

and did this 并做到了

        var timer = $.Deferred();
        setTimeout(timer.resolve, 5000);
        var jaax = getData().done();
        $.when(timer, jaax).done(this.Seven());

This doesn't work since it calls function Seven anyways while its posting 这是行不通的,因为它在发布时仍然调用功能 Seven

I also tried this Ajax call function after success : 成功之后,我还尝试了此Ajax调用功能

      $.ajax({ type: "POST",
         data: JSON.stringify(test), 
         url: "/api/answers/", 
         contentType: "application/json" ,
         success: Seven
          } ); }

And that just gave my soul pain as it calls function Seven while posting, What am i missing? 这给我的灵魂带来了痛苦,因为它在发布时调用了功能七,我想念的是什么? why is it not working? 为什么它不起作用?

try this 尝试这个

getData().done(function(results){
    // results are what retuned from the server
    Seven();
});

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

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