Skip to content

Instantly share code, notes, and snippets.

@bdelacretaz
Last active June 11, 2024 09:52
Show Gist options
  • Save bdelacretaz/34092425efc3fa4f68467a4d8d2ad11b to your computer and use it in GitHub Desktop.
Save bdelacretaz/34092425efc3fa4f68467a4d8d2ad11b to your computer and use it in GitHub Desktop.
HTML page that tests Synchronous XHR
<!DOCTYPE html>
<html lang="en">
<head>
<title>Testing Sync XHR</title>
</head>
<body>
<h1>Testing <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest_API/Synchronous_and_Asynchronous_Requests">Sync XHR</a></h1>
<div id="msg">This is the msg</div>
<pre id="pre">Response comes here.</pre>
<script>
const msg = document.querySelector('#msg');
const pre = document.querySelector('#pre');
const url = "https://httpbin.org/delay/2";
msg.textContent = `Requesting ${url}`;
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.send(null);
if (request.status === 200) {
msg.textContent = `${url} successful`;
document.querySelector('#pre').textContent = request.responseText;
} else {
msg.textContent = `${url} returns ${request.status}`;
pre.textContent = 'ERROR';
}
</script>
</body>
</html>
@bdelacretaz
Copy link
Author

Can also be tested directly in the Web Console:

var request = new XMLHttpRequest();
request.open('GET','https://httpbin.org/delay/3',false);
request.send(null);
if(request.status == 200 && request.readyState == 4) {
 console.log('XMLHttpRequest.send works as expected');
} else {
 console.log('Failed: status', request.status, 'readyState', request.readyState);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment