Gracias, Wikipedia.
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 2 3 var Ajax = function() {4  5  this._xhr = null;6  this.url = '';7  this.method = 'GET';8  this.async = true;9  this.retries = 3;10  this.timeout = 1000; // milliseconds11  this.data = null;12  this.aborted = false;13  this.callbacks = ({14  progress:null,15  done:null,16  error:null,17  presend:null18  });19  20 };21 22 Ajax.prototype.retry = function() {23  this.retries--;24  var that = this;25  if (this.aborted) {26  // Yet finished27  } else if (null !== this._xhr && that._xhr.readyState === XMLHttpRequest.DONE && that._xhr.status != 0) {28  // Yet finished29  } else {30  null === this._xhr || this._xhr.abort();31  32  var xhr = this._xhr = new XMLHttpRequest();33  xhr.open(this.method, this.url, this.async);34  xhr.onreadystatechange = function(event) {35  if (xhr.readyState == XMLHttpRequest.DONE && xhr.status != 0) {36  if (null != that.callbacks.done) {37  that.callbacks.done(xhr);38  }39  }40  };41  xhr.send(this.data);42  /*43  if (null != that.callbacks.presend)44  that.callbacks.presend(that._xhr);45  46  */47  }48 }49 50 Ajax.prototype.send = function(data) {51  var that = this;52  53  this.data = data;54  this.aborted = false;55  this.retry();56  if (this.retries > 0)57  setTimeout(function() {58  that._xhr.abort();59  that._xhr = null;60  }, this.timeout);61 };62 63 Ajax.prototype.abort = function() {64  this.aborted = true;65  this._xhr.abort();66  if (null != this.callbacks.error)67  this.callbacks.error({'number':2,'description':'Aborted'});68 };69 70 window.addEventListener('load', function(e){71  var a = new Ajax();72  a.url = 'http://www.treeweb.es/request2';73  a.callbacks.done = function(xhr) {74  logger.log(xhr);75  };76  a.callbacks.error = function(error) {77  logger.log(error);78  }79  a.callbacks.presend = function(xhr) {80  //logger.log(xhr);81  }82  a.send();83  84  logger.show();85 }, true);86 87 88 89 


Este ShareCode tiene versiones:
  1. Ajax Refactor v0.17 ... (14/12/2012)
  2. Ajax Refactor ... (24/04/2013)
  3. Ajax Refactor ... (24/04/2013)
  4. Ajax Refactor ... (24/04/2013)
  5. Ajax Refactor ... (24/04/2013)
  6. Ajax Refactor ... (24/04/2013)
Enlace
El enlace para compartir es: