Skip to content

Instantly share code, notes, and snippets.

@velocity-360
Last active June 28, 2020 01:37
Show Gist options
  • Save velocity-360/459f4e4fe7e75ea8a63da9de3da8f53d to your computer and use it in GitHub Desktop.
Save velocity-360/459f4e4fe7e75ea8a63da9de3da8f53d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head>
<title>XHR Example</title>
<link rel="shortcut icon" href="https://lh3.googleusercontent.com/MpnmfR3zYPTojA03QbhGgwIHucmH5s4G2vbIlADYhODL488MQt_BuVztv9LYmJbhwkScYN0DIXuh4d10YMsLbWXT">
</head>
<body>
<div>
<h1>Select Reddit Sub</h1>
<div id="list"></div>
</div>
<script>
(function(){
var xhr = new XMLHttpRequest()
xhr.open('GET', '/api/endpoint')
// This will be called after the response is received
xhr.onload = function() {
if (xhr.status != 200) { // status code should be "200" otherwise something went wrong.
console.log('ERROR. Something went wrong.')
return
}
var payload = JSON.parse(xhr.response)
var data = payload.data // data is array of Reddit subs returned from "endpoint.js"
console.log(JSON.stringify(data))
var html = '<ol>'
data.forEach(function(sub){
html += '<a href="/axios/'+sub.path+'">'+sub.name+'</a><br />'
})
html += '</ol>'
document.getElementById('list').innerHTML = html
}
xhr.onError = function() {
console.log('Request failed')
}
xhr.send()
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment