Created
September 7, 2016 20:48
-
-
Save max-dark/ff219b46fb7bf73f33ea68499a5a2327 to your computer and use it in GitHub Desktop.
Simple XMLHttpRequest wrapper
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
/** | |
* 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