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
- new Ajaxito (‘ajax.php’, ‘alertResponse’, ‘var1=3&var2=test’);
- function alertResponse(request){
- alert(request.responseText);
- }
Here is the code.
- /*
- Ajaxito
- By Rene Lopez Caballero <http://www.mywebexperiences.com/javascript/ajaxito/>
- Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
- */
- function Ajaxito(url, callback, post) {
- this.onStateChange = function(){
- if (this.transport.readyState == 4 && this.transport.status == 200) {
- if (this.callback) setTimeout(this.bind(function(){this.callback(this.transport);},this), 10);
- this.transport.onreadystatechange = function(){};
- }
- }
- this.bind = function(caller,obj){ return function(){ return caller.apply(obj,arguments); } }
- this.transport = window.ActiveXObject ? new ActiveXObject(‘Microsoft.XMLHTTP’) : ( window.XMLHttpRequest ? new XMLHttpRequest() : false )
- this.callback = callback || null;
- this.transport.open((post?‘post’:‘get’), url, true);
- this.transport.onreadystatechange = this.bind(this.onStateChange,this);
- if(post){
- this.transport.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’);
- if (this.transport.overrideMimeType) this.transport.setRequestHeader(‘Connection’, ‘close’);
- }
- this.transport.send(post);
- }