Created
August 13, 2017 06:59
-
-
Save alexcu/383cd72ce44252a4e92e551aaee273dc to your computer and use it in GitHub Desktop.
AJAX in 14 lines of JavaScript
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
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