views:

34

answers:

1

First, the code in question:

ajax = function(url, cb)
 {
    xhr = (window.XMLHttpRequest)
     ? new XMLHttpRequest()
     : new ActiveXObject('Microsoft.XMLHTTP');
    xhr.onreadystatechange = function()
     {
     if (xhr.readyState == 4 && xhr.status == 200)
      {
      cb(xhr.responseText);
      };
     } 
    xhr.open('get', url, true);
    xhr.send();
 };

Now, I know I could easily opt for a library solution, but at the moment I'm trying to roll together a far more lightweight library for personal use; is this function missing anything crucial?

+1  A: 

That's pretty basic but looks complete without actually trying it. You might want to consider how you are going to handle errors. You might also want to make it where you could POST or set headers if needed. If, however, all you need is to call RESTful URLs to get your data, that should work.

DMKing