Created
March 24, 2018 00:02
-
-
Save djorborn/fd347b2f29bb12f7e1ad6206a2ee248d to your computer and use it in GitHub Desktop.
My ajax request helper, like the one from JQUERY just very simple
This file contains hidden or 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, options) { | |
options.url = url; | |
var xhr = new XMLHttpRequest() | |
xhr.open((!options.type ? 'GET': options.type), options.url, true) | |
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded") | |
xhr.onreadystatechange = function () { | |
if (this.readyState == 4) { | |
options.success(this.response) | |
} | |
} | |
xhr.send("json=" + JSON.stringify(options.data)) | |
} | |
/* | |
ajax(url, { | |
type: "POST", | |
data: JSON OBJ, | |
success: function(response) { | |
console.log(res); | |
} | |
}) | |
*/ | |
//I want to figure out how to not need the "json" part but still use application/x-www-form-urlencoded like JQUERY does |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment