2

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?

6
  • What use case do you have for synchronous requests? In general they are not recommended (certain browsers even state this in their console when a synchronous request is sent) Commented May 3, 2019 at 10:42
  • Just put false as the 3rd parameter to open, but a big warning, if you use this feature not inside a WebWorker it might stop working in the future. Commented May 3, 2019 at 10:42
  • I suppose the question is more about alternatives to XMLHttpRequest, no? Commented May 3, 2019 at 10:43
  • Possible duplicate of JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." Commented May 3, 2019 at 10:46
  • @Keith Thanks. where do you recommend to look up the API usages? Commented May 3, 2019 at 11:18

1 Answer 1

5

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(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.