Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active January 8, 2021 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Integralist/a66a29561f6544297a5ad41a8208193c to your computer and use it in GitHub Desktop.
Save Integralist/a66a29561f6544297a5ad41a8208193c to your computer and use it in GitHub Desktop.
[Simple XHR function] #js #javascript #xhr #ajax
function request(url, params, callback) {
/*
var params = 'pass=' + encodeURIComponent(password.value) +
'&encval=' + encodeURIComponent(encval.value) +
'&code=' + encodeURIComponent(code.value) +
'&username=' + encodeURIComponent(username.value) +
'&_xsrf=' + encodeURIComponent(xsrf);
*/
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 500) {
return callback({
'state': 'error',
'message': 'There was a problem processing this request.'
});
}
var response = JSON.parse(xhr.responseText);
return callback(response);
}
};
xhr.send(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment