Skip to content

Instantly share code, notes, and snippets.

@alexcu
Created August 13, 2017 06:59
Show Gist options
  • Save alexcu/383cd72ce44252a4e92e551aaee273dc to your computer and use it in GitHub Desktop.
Save alexcu/383cd72ce44252a4e92e551aaee273dc to your computer and use it in GitHub Desktop.
AJAX in 14 lines of JavaScript
function ajax(url, callback, data, x) {
try {
x = new(this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0')
x.open(data ? 'POST' : 'GET', url, 1)
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
x.setRequestHeader('Content-type', 'application/json')
x.onreadystatechange = function () {
x.readyState > 3 && callback && callback(x.responseText, x)
}
x.send(JSON.stringify(data))
} catch (e) {
window.console && console.log(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment