Last active
January 8, 2021 10:34
-
-
Save Integralist/a66a29561f6544297a5ad41a8208193c to your computer and use it in GitHub Desktop.
[Simple XHR function] #js #javascript #xhr #ajax
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 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