Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.39 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var Jaxy = function() {
  2.     this.xhr = new XMLHttpRequest;
  3. };
  4. Jaxy.prototype.get = function(options) {
  5.     if (typeof options.url === 'undefined') return;
  6.    
  7.     var self = this;
  8.     var url = options.url || '';
  9.     var success = options.success || null;
  10.     var error = options.error || null;
  11.    
  12.     this.xhr.onreadystatechange = function() {
  13.         if (this.readyState === 4) {
  14.             if (self.isSuccess(this.status)) {
  15.                 success(this.responseText);
  16.             } else {
  17.                 error(this.responseText);
  18.             }
  19.         }
  20.     };
  21.     this.xhr.open("GET", url, true);
  22.     this.xhr.send(null);
  23. }
  24. Jaxy.prototype.post = function(options) {
  25.     if (typeof options.url === 'undefined') return;
  26.    
  27.     var self = this;
  28.     var url = options.url || '';
  29.     var success = options.success || null;
  30.     var error = options.error || null;
  31.    
  32.     this.xhr.onreadystatechange = function() {
  33.         if (this.readyState === 4) {
  34.             if (self.isSuccess(this.status)) {
  35.                 success(this.responseText);
  36.             } else {
  37.                 error(this.responseText);
  38.             }
  39.         }
  40.     };
  41.     this.xhr.open("POST", url, true);
  42.     this.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  43.     this.xhr.send(options.data);
  44. }
  45. Jaxy.prototype.isSuccess = function(code) {
  46.     if (code >= 200 && code < 400) {
  47.         return true;
  48.     }
  49.     return false;
  50. }