简体   繁体   English

等待 function 内部的多个 ajax 调用?

[英]Await multiple ajax calls inside function?

Context: Making an ajax heavy page that changes values in different selectors based on what prior selectors have chosen.上下文:制作 ajax 重页,根据先前选择器选择的内容更改不同选择器中的值。 Working on making a "repopulate" option based on prior entries.致力于根据先前的条目制作“重新填充”选项。

When selector 1 is changed, an ajax call is made that populates both selector 2 and 3. Selector 1's options never change. When selector 1 is changed, an ajax call is made that populates both selector 2 and 3. Selector 1's options never change.


When you "repopulate" from a prior entry, the code starts by changing selector 1's value and then activates a change event on selector 1.当您从先前的条目“重新填充”时,代码首先更改选择器 1 的值,然后激活选择器 1 上的更改事件。

function repopulateFromEntry(event)  {
    // We want the children of the parent TR.
    // <tr>
    //  <td>...</td>
    //  ...
    //  <td><button></td>
    // <tr>
    let td_list = event.target.parentElement.parentElement.children;

    $('#selector1').val(td_list[0].innerHTML);
    $('#selector1').change();
    // Do other things that rely on prior to be finished
    // Problem is here.
};

Selector 1's change event looks like this.选择器 1 的更改事件如下所示。

async function executeAjax(url, success) {
    return await $.ajax({
        url: url,
        type: "GET",
        success: success
    });
}

$('#selector1').change(async function(e) {
    await executeAjax('api/selector2' + $("#selector1").val(), function() {
        // Set selector2 from ajax data
    });
    await executeAjax('api/selector3' + $("#selector1").val(), function() {
        // Set selector3 from ajax data
    });
});

After the selectors options are set based on selector1's value, it then goes in and selects the correct value for selectors 2 and 3.根据 selector1 的值设置选择器选项后,它会进入并为选择器 2 和 3 选择正确的值。


My problem is that the reselection of values for selectors 2 and 3 is getting called before the options are fully populated to the selectors, making the reselection fail.我的问题是选择器 2 和 3 的值的重新选择在选项完全填充到选择器之前被调用,从而导致重新选择失败。

I'm clearly missing something from the async/await/ajax section to keep it from continuing without both calls being done, but I can't seem to see what my issue is.我显然在 async/await/ajax 部分中遗漏了一些东西,以防止它在没有完成两个调用的情况下继续,但我似乎看不出我的问题是什么。

OK, so I used async/await for the $.ajax call, and then in your change event handler, I used.then method to act on the resulting data.好的,所以我对 $.ajax 调用使用了 async/await,然后在您的更改事件处理程序中,我使用了.then 方法来处理结果数据。 (Also could have used async await in the event handler, but since you had that originally and it wasn't working I opted for promise instead). (也可以在事件处理程序中使用异步等待,但是由于您最初拥有它并且它不起作用,因此我选择了 promise 代替)。

I'm pretty sure this should work, but if not, please let me know what the console is showing.我很确定这应该可以,但如果不行,请告诉我控制台显示的内容。

NOTE You may need to console.log result and pull out the data you are looking for before setting the value of each selector.注意在设置每个选择器的值之前,您可能需要 console.log 结果并提取您要查找的数据。 You can do that inside the.then method.您可以在 .then 方法中执行此操作。

async function executeAjax(url) {

    let result;

    try { 
        result = await $.ajax({
            url: url,
            type: "GET"
        });

        return result;

    } catch (error) {
        console.log(error);
    }
}

$('#selector1').change(function(e) {

    executeAjax('api/selector2' + $("#selector1").val())
    .then((result) => { 
        // console.log(result);  <-- may need to find and pull the data you are looking for out of result
        // let myVal = result[?]['something'].orother;
        $("#selector2").val(result); 
    });

    executeAjax('api/selector3' + $("#selector1").val())
    .then((result) => {
        // console.log(result);  <-- may need to find and pull the data you are looking for out of result
        // let myVal = result[?]['something'].orother;
        $("#selector3").val(result);
    });

});

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

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