Nuestro conocimiento compartido. Nuestro tesoro compartido. Wikipedia.
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 var Ajax = function () {2  3  this.xhr = null;4  this.data = null;5  this.method = 'get';6  this.url = null;7  this.async = true;8  this.timeout = 3000;9  this.callback = {10  presend : null,11  progress : null,12  done : null,13  error : null14  };15  16  17  18 };19 20 /**21  * Create xhr and send data22 */23 Ajax.prototype.send = function() {24  25  var xhr = this.xhr = new XMLHttpRequest({mozSystem:true});26  27  xhr.open(this.method, this.url, this.async);28  xhr.timeout = this.timeout;29  xhr.setRequestHeader('Accept', 'application/json');30  31  xhr.timeout = this.timeout;32  /* Uncomment for nightly: 33  if (this.token_auth != '') 34  xhr.setRequestHeader('x-auth-token', this.token_auth);35  //*/36  37  xhr.onreadystatechange = function(event) {38  if (xhr.readyState == XMLHttpRequest.DONE) {39  if (xhr.status == 0) {40  41  } else {42  43  }44  }45  };46  47  xhr.onerror = function(event) {48  logger.log({'error':event,'xhr':xhr});49  };50  51  xhr.ontimeout = function (event) {52  logger.log({'timeout':event,'xhr':xhr});53  };54  55  if(this.data === null) {56  xhr.send();57  } else if(typeof this.data === 'string') {58  xhr.setRequestHeader('Content-length', this.data.length);59  xhr.send(this.data);60  } else if(typeof this.data === 'object') {61  if(this.data.toString() == '[object Object]') {62  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');63  var s = '';64  var key;65  for(key in this.data) {66  s += key + '=' + encodeURIComponent(this.data[key]) + '&';67  }68  xhr.setRequestHeader('Content-length', s.length);69  xhr.send(s);70  } else if(this.data.toString() == '[object FormData]') {71  xhr.send(this.data);72  }73  }74 };75 76 /**77  * Añade los datos78 */79 Ajax.prototype.setData = function(data) {80  this.data = data;81 };82 83 84 85 /* Test */86 window.addEventListener('load', function(){87  var a = new Ajax();88  a.url = 'http://www.treeweb.es/request';89  a.send();90 }, true);91 92 93 94 95 96 /*97 Ajax.prototype.a = function() {};98 99 Ajax.prototype.a = function() {};100 101 Ajax.prototype.a = function() {};102 103 Ajax.prototype.a = function() {};104 105 Ajax.prototype.a = function() {};106 107 Ajax.prototype.a = function() {};108 109 110 */111 112 


Este ShareCode tiene versiones:
  1. ... (31/01/2013)
  2. ... (24/04/2013)
  3. ... (24/04/2013)
  4. ... (24/04/2013)
  5. ... (24/04/2013)
  6. ... (24/04/2013)
Enlace
El enlace para compartir es: