Skip to content

Instantly share code, notes, and snippets.

@ilyaashapatov
Created February 16, 2017 11:14
Show Gist options
  • Save ilyaashapatov/0e1e68e27c31da68dcdd260c6d8ff5f6 to your computer and use it in GitHub Desktop.
Save ilyaashapatov/0e1e68e27c31da68dcdd260c6d8ff5f6 to your computer and use it in GitHub Desktop.
function xhr($url, $type, $data) {
var xmlhttp;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
};
};
if (!xmlhttp && typeof XMLHttpRequest !== undefined) {
xmlhttp = new XMLHttpRequest();
};
xmlhttp.open($type, $url, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
// this code
}
};
if ($type === 'POST') {
xmlhttp.send($data);
} else {
xmlhttp.send(null);
}
}
// init
xhr('link', 'POST', 'params').then(
function(response) {}, //success state
function(error) {} //fail state
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment