How to wait for ajax success handler to finish before executing another

前端 未结 4 1290
梦如初夏
梦如初夏 2021-01-18 21:50

I have the following scenario in my javascript:

  1. Ajax loads a record.
  2. Based on the return value of that record, another ajax call grabs a html form tha
4条回答
  •  北海茫月
    2021-01-18 22:47

    You can use jQuery promises, i.e.:

    var loadingData = $.get(loadDataUrl);
    var loadingHtml = $.get(grabHtmlUrl);
    $.when(loadingData, loadingHtml).done(function(loadDataResult, loadHtmlResult){
         //add the html to the dom
         //use data to update the dom as you please       
    });
    

    Note: $.get is just a version of $.ajax that performs a get request

    Sources: http://api.jquery.com/deferred.promise/

    http://api.jquery.com/jquery.get/

    http://api.jquery.com/jquery.ajax/

    http://api.jquery.com/jquery.when/

提交回复
热议问题