最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery asyncawait ajax call - Stack Overflow

programmeradmin3浏览0评论

I'm currently using 3 ajax call methods (3 of them are executing back-to-back). I must have a time delay in between the second ajax call and the third one. If I add "async:false" in the second ajax, everything works like a charm. However, I found out that this is really a terrible practice and shouldn't be using. So I decided to try an async/await. This is my first time, so have no luck. I would be really grateful if you guys could add some explanations so I can learn. Thank you so much.

//This is the second ajax method that I've been trying to use async/await
async function InsertAssignments(data) {
    var insertNewData = api + "/Point/insert_data/";
    await $.ajax({
        type: "POST",
        url: insertNewData + data,
        dataType: "json",
        data: data,
        timeout: 30000,
        success: function (data) {
            $("#mainGrid").data("kendoGrid").dataSource.read();
            $("#ListBox1").data("kendoListBox").dataSource.read();
            $("#ListBox2").data("kendoListBox").dataSource.read();
        },
        error: function (xhr, status, error) {
            $('#gridMessage').html(xhr.responseText).css("color", "red");
        }
    });
}

and then I'm calling InsertAssignments(data) somewhere.

I'm currently using 3 ajax call methods (3 of them are executing back-to-back). I must have a time delay in between the second ajax call and the third one. If I add "async:false" in the second ajax, everything works like a charm. However, I found out that this is really a terrible practice and shouldn't be using. So I decided to try an async/await. This is my first time, so have no luck. I would be really grateful if you guys could add some explanations so I can learn. Thank you so much.

//This is the second ajax method that I've been trying to use async/await
async function InsertAssignments(data) {
    var insertNewData = api + "/Point/insert_data/";
    await $.ajax({
        type: "POST",
        url: insertNewData + data,
        dataType: "json",
        data: data,
        timeout: 30000,
        success: function (data) {
            $("#mainGrid").data("kendoGrid").dataSource.read();
            $("#ListBox1").data("kendoListBox").dataSource.read();
            $("#ListBox2").data("kendoListBox").dataSource.read();
        },
        error: function (xhr, status, error) {
            $('#gridMessage').html(xhr.responseText).css("color", "red");
        }
    });
}

and then I'm calling InsertAssignments(data) somewhere.

Share Improve this question asked Feb 22, 2020 at 7:12 davisdavis 3811 gold badge5 silver badges18 bronze badges 3
  • That looks like one request. Where are the other two? – CertainPerformance Commented Feb 22, 2020 at 7:14
  • Can you respond? It's unclear which 3 requests you're referring to – CertainPerformance Commented Feb 22, 2020 at 7:44
  • The other two are working just fine. I do not need add async:false in them. I only just want async/await in this ajax method. – davis Commented Feb 22, 2020 at 16:44
Add a comment  | 

1 Answer 1

Reset to default 17
  1. Async / await requires functions to return a promise.
  2. jQuery $.ajax() can be used in two ways: with callbacks and promises. Your code is using a callback, not a promise.

Wrap $.ajax() in a function like this:

async function doAjax(url, params = {}, method = 'POST') {
    return $.ajax({
      url: url,
      type: method,
      dataType: 'json',
      data: params
    });
}

Then use await doAjax() whenever you are making an ajax call.

async function InsertAssignments(data) {
  const insertNewData = api + '/Point/insert_data/';
  try {
    // You can make multiple ajax calls
    // Response data is stored in result
    const result = await doAjax(insertNewData, data);
  } catch (error) {
    console.log('Error! InsertAssignments:', error);
    $('#gridMessage').html(error.message).css('color', 'red');
  }
}
发布评论

评论列表(0)

  1. 暂无评论