Skip to content

Instantly share code, notes, and snippets.

@Scemist
Last active May 2, 2022 14:32
Show Gist options
  • Save Scemist/30cd78636c71afa7ee7015cd93527e29 to your computer and use it in GitHub Desktop.
Save Scemist/30cd78636c71afa7ee7015cd93527e29 to your computer and use it in GitHub Desktop.
Elegant Ajax in JS with XMLHttpRequest
const xhr = new XMLHttpRequest()
xhr.open('GET', 'server.php')
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded ')
xhr.onreadystatechange = _ => {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText)
} else {
console.log('Loading')
}
}
xhr.send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment