Coding Examples of Synchronous and Asynchronous AJAX

Short Answer
Views 32

Answer:

Synchronous AJAX Example


var xhrSync = new XMLHttpRequest();
xhrSync.open('GET', 'example-url', false);
xhrSync.send();

// Process the response

Asynchronous AJAX Example


var xhrAsync = new XMLHttpRequest();
xhrAsync.open('GET', 'example-url', true);
xhrAsync.send();

// Process the response

Handling Responses


xhrAsync.onreadystatechange = function() {
  if (xhrAsync.readyState === 4 && xhrAsync.status === 200) {
    // Process the asynchronous response
    var responseData = xhrAsync.responseText;
  }
};

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of AJAX, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.