how to wait for an ajax call to return

前端 未结 6 840
天涯浪人
天涯浪人 2021-01-12 20:00

I\'m trying to use JQuery although i\'m struggling to successfully wait for a ajax call to succeed before executing further code. Is there a way to wait for an ajax to call

6条回答
  •  花落未央
    2021-01-12 20:40

    jQuery's ajax methods have a success handler.

    You should put your code that you want to fire on success in a method attached to this handler.

    Consider the example given on the jQuery website:

    $.ajax({
      url: "test.html",
      context: document.body,
      success: function(){
        $(this).addClass("done");
      }
    });
    

    You can see here that there's a success handler with a method attached. This method will execute when the ajax method returns succesfully.

    As has been pointed out in other answers and the comment below, you can now use deferreds instead of this simple success handeler. This allows you to attach multiple actions to each given event.

提交回复
热议问题