2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

複数のAjaxを順番に実行させる

Last updated at Posted at 2021-04-24

対象読者

  • async, awaitをソースに書くと死んじゃう病の人
  • jQueryの$.ajaxを利用する人

バージョン

jQuery: 3.6.0

こうする

<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<script>
// 順次
hogeAjax().then(function(data) {
    console.log(data.args.param);
    return fugaAjax();
}).then(function(data) {
    console.log(data.args.param);
}).catch(function(e) {
    console.log(e);
});

function hogeAjax() {
    return $.ajax({
        url: "https://httpbin.org/delay/5",
        type: "GET",
        data: {
            param: "ほげほげ",
        }
    });
}

function fugaAjax() {
    return $.ajax({
        url: "https://httpbin.org/delay/1",
        type: "GET",
        data: {
            param: "ふがふが",
        }
    });
}
</script>

類似記事

複数のAjaxのレスポンスを全て受け取ったら実行させる
複数のAjaxの最初に受け取ったレスポンスに対して実行させる

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?