简体   繁体   中英

How can we send a HTTP request synchronously?

If I am correct, "A" in "AJAX" means sending a HTTP request asynchronously without waiting for a HTTP response.

I learn that we can send an asynchronous HTTP request by XMLHttpRequest , for example:

function handleButtonPress(e) { 
    var httpRequest = new XMLHttpRequest(); 
    httpRequest.onreadystatechange = handleResponse; 
    httpRequest.open("GET", e.target.innerHTML +  ".html"); 
    httpRequest.send(); 
} 

How can we send a HTTP request synchronously?

The third param in open function is for async request sending. You can set it to false for a synchronous request

function handleButtonPress(e) { 
    var httpRequest = new XMLHttpRequest(); 
    httpRequest.onreadystatechange = handleResponse; 
    httpRequest.open("GET", e.target.innerHTML +  ".html", false); 
    httpRequest.send(); 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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