✏️ Explanatory Question

Coding Examples of Synchronous and Asynchronous AJAX

👁 102 Views
📘 Detailed Answer
🟢 Easy
102
Total Views
7
Related Qs
0%
Progress
💡

Answer with Explanation

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;
  }
};