Skip to content

Instantly share code, notes, and snippets.

@max-dark
Created September 7, 2016 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-dark/ff219b46fb7bf73f33ea68499a5a2327 to your computer and use it in GitHub Desktop.
Save max-dark/ff219b46fb7bf73f33ea68499a5a2327 to your computer and use it in GitHub Desktop.
Simple XMLHttpRequest wrapper
/**
* Simple XMLHttpRequest wrapper
*
* @constructor
* @this {Ajax}
*/
var Ajax = function () {
/**
* @type {XMLHttpRequest}
*/
this.sender = new XMLHttpRequest();
};
Ajax.prototype = ({
/**
* send new request
* @param options {Object}
* @this {Ajax}
*/
send: function (options) {
this.sender.open(options.method, options.url, true);
if ('POST' == options.method) {
this.sender.setRequestHeader("Content-Type", options.type);
}
this.sender.onload = options.on_load;
this.sender.send(options.data);
},
/**
* send 'GET' request
* @param options {Object}
* @this {Ajax}
*/
get: function (options) {
options.method = "GET";
this.send(options);
},
/**
* send 'POST' request
* @param options {Object}
* @this {Ajax}
*/
post: function (options) {
options.method = "POST";
options.type = options.type || "application/x-www-form-urlencoded";
this.send(options);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment