DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

kevin has posted 2 posts at DZone. View Full User Profile

Super Lightweight Ajax Function

02.21.2009
| 5708 views |
  • submit to reddit
        This is part of my super insanely lightweight Javascript framework vX (which is currently 3kb and has many features of the really large frameworks, like animation, ajax, events, and a whole lot more). Learn more about the framework at http://code.google.com/p/vxjs/

vX=function(u,f,d,x){x=this.ActiveXObject;x=new(x?x:XMLHttpRequest)('Microsoft.XMLHTTP');x.open(d?'POST':'GET',u,1);x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.onreadystatechange=function(){x.readyState>3&&f?f(x.responseText,x):0};x.send(d)}

use vX(URL[, Callback, Post Parameters]). To simply do an ajax GET request you can do vX(url) if you want to do a simple GET with the second parameter as a  function (or a reference to one) which will be called when the request is completed. The callback will be called with two parameters, the first one with the .responseText attribute of the XHR object, and the second with the actual xhr object. to do a simple POST request you do vX(url, function(){}, "param=blah&etc=cool").