简体   繁体   English

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

[英]VanillaJS vs JQuery - wait handler until two async requests are done

I have used JQuery since a long time and i am familar with the AJAX-Calls in it. 我已经使用JQuery很久了,我熟悉其中的AJAX-Calls。 I often had the situation where i had to wait until multiple requests have been finished and continue after that with the results. 我经常遇到这样的情况,我必须等到多个请求完成后再继续执行结果。

The JQuery syntax was the following: 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();
        ...
    });

How can you do this in VanillaJS? 您如何在VanillaJS中做到这一点?

Here is an example using the new vanilla JS fetch API 这是使用新的香草JS提取API的示例

fetch('URL', {
    method: "POST/PUT/GET/DELETE",
    body: JSON.stringify({
       name: Name,
       otherData : otherData
    }),`enter code here`
    headers: {"content-type": "application/json"}
})
.then(res => res.json())
.then(response => {
    //do what you want with the response here
})

For a GET request you can opt-out the body in fetch like 对于GET请求,您可以选择退出,例如

fetch('URL', {
    method: "GET",
    headers: {"content-type": "application/json"}
})
.then(res => res.json())
.then(response => {
    //do what you want with the response here
})

 fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => { document.getElementById("userID").innerHTML = json.userId; document.getElementById("title").innerHTML = json.title; document.getElementById("completed").innerHTML= json.completed; }) 
 <div>The User ID is : </div> <div id="userID"> </div> <div>The Title is : </div> <div id="title"> </div> <div>Completed : </div> <div id="completed"> </div> 

Compare this one with your AJAX request: 将此与您的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**/);

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

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