Ajaxito

Ajaxito is a pretty small ajax class, based on moo.ajax and microajax, it only weights 1kb uncompressed, to use it, just call it like:

new Ajaxito( url, callback, post );

  • url: The url to get the data
  • callback: The function to execute once the ajax request has been done
  • post:This last parameter is optional, here you can include the post body

Here is an very basic example on how to use it

  1.  
  2. new Ajaxito (‘ajax.php’, ‘alertResponse’, ‘var1=3&var2=test’);
  3. function alertResponse(request){
  4.         alert(request.responseText);
  5. }
  6.  

Download it here

Here is the code.

  1.  
  2. /*
  3. Ajaxito
  4. By Rene Lopez Caballero  <http://www.mywebexperiences.com/javascript/ajaxito/>
  5. Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. */
  7. function Ajaxito(url, callback, post) {
  8.         this.onStateChange = function(){
  9.                 if (this.transport.readyState == 4 && this.transport.status == 200) {
  10.                         if (this.callback) setTimeout(this.bind(function(){this.callback(this.transport);},this), 10);
  11.                         this.transport.onreadystatechange = function(){};
  12.                 }
  13.         }
  14.         this.bind = function(caller,obj){ return function(){ return caller.apply(obj,arguments); } }
  15.         this.transport = window.ActiveXObject ? new ActiveXObject(‘Microsoft.XMLHTTP’) : ( window.XMLHttpRequest ? new XMLHttpRequest() : false )
  16.         this.callback = callback || null;
  17.         this.transport.open((post?‘post’:‘get’), url, true);
  18.         this.transport.onreadystatechange = this.bind(this.onStateChange,this);
  19.         if(post){
  20.                 this.transport.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’);
  21.                 if (this.transport.overrideMimeType) this.transport.setRequestHeader(‘Connection’, ‘close’);
  22.         }
  23.         this.transport.send(post);
  24. }
  25.  

Leave a Reply

Your email address will not be published. Required fields are marked *