Last active
June 5, 2018 11:25
-
-
Save githubrakesh/7cad38736f4e0f4b8ed03c7e4a9af7e7 to your computer and use it in GitHub Desktop.
check if a network connection timed out during an AJAX call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<h2>XMLHttpRequest : Status and ReadyState</h2> | |
<button type="button" onclick="loadData()">Load Data</button> | |
<script> | |
function loadData() { | |
var xhr = new XMLHttpRequest(); | |
var url = | |
"https://v181.iqsmartapp.com/enterprisedesktop/Dashboard/AppsMode/GetAppsModeStatistics?sessionid=71c67eda1c044aa3bc86c8a8b92ec01a" | |
xhr.open('POST', url); | |
xhr.setRequestHeader('Content-type', 'application/json'); | |
var formdata = { | |
userid: 139, | |
projectid: '' | |
}; | |
xhr.send(formdata); | |
xhr.onreadystatechange = function () { | |
console.group("Ready :"); | |
console.log("State " + xhr.readyState); | |
console.log("Status " + xhr.status); | |
console.groupEnd("Ready:"); | |
if (xhr.readyState === 4 && xhr.status === 200) { | |
// All good - | |
console.log("Cheers! All Good - Go ahead and have your pancake"); | |
alert(xhr.responseText); | |
} | |
if (xhr.readyState === 4 && xhr.status === 0) { | |
// Broken Network :-( ) | |
console.log("Clear up the mess and bake your cake again!"); | |
window.setTimeout(() => { | |
window.open("http://www.google.com", "_blank"); // any url | |
setTimeout(() => { | |
loadData(); | |
}, 2000); | |
}, 2000); | |
} | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment