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  26  alert(navigator.onLine);27  28  var xhr = this.xhr = new XMLHttpRequest({mozSystem:true});29  30  xhr.open(this.method, this.url, this.async);31  xhr.timeout = this.timeout;32  xhr.setRequestHeader('Accept', 'application/json');33  34  xhr.timeout = this.timeout;35  /* Uncomment for nightly: 36  if (this.token_auth != '') 37  xhr.setRequestHeader('x-auth-token', this.token_auth);38  //*/39  40  xhr.onreadystatechange = function(event) {41  if (xhr.readyState == XMLHttpRequest.DONE) {42  if (xhr.status == 0) {43  logger.log({'timeout':event,'xhr':xhr});44  } else {45  logger.log({'done':event,'xhr':xhr});46  }47  }48  };49  50  xhr.onerror = function(event) {51  logger.log({'error':event,'xhr':xhr});52  };53  54  if(this.data === null) {55  xhr.send();56  } else if(typeof this.data === 'string') {57  xhr.setRequestHeader('Content-length', this.data.length);58  xhr.send(this.data);59  } else if(typeof this.data === 'object') {60  if(this.data.toString() == '[object Object]') {61  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');62  var s = '';63  var key;64  for(key in this.data) {65  s += key + '=' + encodeURIComponent(this.data[key]) + '&';66  }67  xhr.setRequestHeader('Content-length', s.length);68  xhr.send(s);69  } else if(this.data.toString() == '[object FormData]') {70  xhr.send(this.data);71  }72  }73 };74 75 /**76  * Añade los datos77 */78 Ajax.prototype.setData = function(data) {79  this.data = data;80 };81 82 83 84 /* Test */85 window.addEventListener('load', function(){86  var a = new Ajax();87  a.url = 'http://www.treeweb.es/request';88  a.send();89 }, true);90 91 92 93 94 95 /*96 Ajax.prototype.a = function() {};97 98 Ajax.prototype.a = function() {};99 100 Ajax.prototype.a = function() {};101 102 Ajax.prototype.a = function() {};103 104 Ajax.prototype.a = function() {};105 106 Ajax.prototype.a = function() {};107 108 109 */110 111 


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