为了账号安全,请及时绑定邮箱和手机立即绑定

VanillaJS vs JQuery - 等待处理程序,直到完成两个异步请求

VanillaJS vs JQuery - 等待处理程序,直到完成两个异步请求

PIPIONE 2021-10-14 12:44:23
我已经使用 JQuery 很长时间了,我熟悉其中的 AJAX-Calls。我经常遇到这样的情况,我必须等到多个请求完成后才能继续获得结果。JQuery 语法如下:    $.when(        $.ajax({            type: "POST",            url: '/Services/Service.asmx/GetResults1',            contentType: "application/json; charset=utf-8",            dataType: "json",            success: function (msg) {                ...            },            error: function (e) {                console.log('ERROR! ' + e.responseText);            }        }),        $.ajax({            type: "POST",            url: '/Services/Service.asmx/GetResults2',            contentType: "application/json; charset=utf-8",            dataType: "json",            success: function (msg) {                    ...                });            },            error: function (e) {                console.log('ERROR! ' + e.responseText);            }        })    ).done(function (result1, result2) {        // Do s.th. with result1 and result2 what is already        // available here        updateUI();        ...    });你怎么能在 VanillaJS 中做到这一点?
查看完整描述

2 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

将此与您的 AJAX 请求进行比较:


var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

    if (this.readyState == 4 && this.status == 200) {

       receivedJSON = JSON.parse(xhttp.responseText);

       //Your success: function here...

    }else{

       //Your error: function here...

    }

};

xhttp.open("POST","/Services/Service.asmx/GetResults1",true);

xhttp.send(/**Your JSON Data**/);


查看完整回答
反对 回复 2021-10-14
  • 2 回答
  • 0 关注
  • 170 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信