
Untitled
By: a guest on
May 8th, 2012 | syntax:
None | size: 1.39 KB | hits: 11 | expires: Never
var Jaxy = function() {
this.xhr = new XMLHttpRequest;
};
Jaxy.prototype.get = function(options) {
if (typeof options.url === 'undefined') return;
var self = this;
var url = options.url || '';
var success = options.success || null;
var error = options.error || null;
this.xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (self.isSuccess(this.status)) {
success(this.responseText);
} else {
error(this.responseText);
}
}
};
this.xhr.open("GET", url, true);
this.xhr.send(null);
}
Jaxy.prototype.post = function(options) {
if (typeof options.url === 'undefined') return;
var self = this;
var url = options.url || '';
var success = options.success || null;
var error = options.error || null;
this.xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (self.isSuccess(this.status)) {
success(this.responseText);
} else {
error(this.responseText);
}
}
};
this.xhr.open("POST", url, true);
this.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.xhr.send(options.data);
}
Jaxy.prototype.isSuccess = function(code) {
if (code >= 200 && code < 400) {
return true;
}
return false;
}