Skip to content

Instantly share code, notes, and snippets.

@juliozuppa
Created April 8, 2017 07:22
Show Gist options
  • Save juliozuppa/0e7bfb61a64aff74fb1b705b69ef370a to your computer and use it in GitHub Desktop.
Save juliozuppa/0e7bfb61a64aff74fb1b705b69ef370a to your computer and use it in GitHub Desktop.
function ajax(method, url, callback) {
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; }
} else { // code for IE6, IE5
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { xmlhttp = false; }
}
}
if (!xmlhttp) { alert("Erro ao criar o objeto XMLHttpRequest"); return; }
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
if (xmlhttp.status === 200) {
callback(xmlhttp.responseText);
} else {
alert("Error" + xmlhttp.statusText);
}
}
}
xmlhttp.open(method, url, true);
xmlhttp.onerror = function () {
alert("Ocorreu algum erro durante a transação ajax!");
};
xmlhttp.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment