Mike West — Web Application Developer

DataRequestor is a JavaScript wrapper for the XMLHttpRequest object that enables the trivial implementation of dynamic interfaces without the painful necessity for a complete page-refresh to talk to the server. In other words: Ajax without the confusing API.

How can you use DataRequestor?

When building applications, the most typical Ajaxy sort of thing I find myself doing is sending off data to a server-side script, and then sticking the resulting HTML directly into some element on the page. DataRequestor makes this process absolutely trivial, and enables quite a bit more functionality with minimal effort.

var req = new DataRequestor();
req.setObjToReplace('objID');
req.getURL('/path/to/my/file.php');

Those three lines build an XMLHtmlRequest object, grab the relevant data from the server, and use that information to replace the innerHTML of an element with id ‘objID’. Trivial, right?

What if we wanted to do something else when the data’s loaded? Instead of replacing an element’s innerHTML, maybe we want to do some crazy JavaScript stuff once the data’s finished loading. Typically, this would involve being annoyed at the XMLHttpRequest object’s rather irritating callback structure. Happily, DataRequestor wraps this complexity in a warm blanket of event-driven goodness:

req.onload = function (data, obj) {
    // Insert crazy JavaScript stuff here!
}

DataRequestor is a solid, stable, and event-driven JavaScript class that you can simply drop into a page and start running with. It makes XMLHTTPRequest’s functionality available in a way that doesn’t make my brain hurt, and hopefully it will do the same for you. Download DataRequestor.js and browse through the code. It’s well commented; use cases and instructions abound.

How can I get it?

You may download the current stable version (1.6) of DataRequestor.js right here. How’s that for convenience?

Future Plans

The landscape has changed a lot since I released DataRequestor in March, 2005. Frameworks have appeared that do everything DataRequestor does, and I certainly don’t have the time or desire to compete with them on a feature-for-feature basis. Happily, I think that’s a feature, not a bug. :)

I believe there’s room for, and demand for, a simple library that does the bare minimum in terms of enabling Ajaxy interactions in existing applications. I think the API that DataRequestor exposes is clear, concise, and trivial to implement. In short, it’s not going to disappear.

In the next couple of weeks, I’ll be making some changes to DataRequestor’s backend in order to leverage some of the incredible work being done on Yahoo’s User Interface library. I’ll be pulling pieces of that library’s code to increase DataRequestor’s overall stability and elegance, while keeping the external interface the same. Just like this 1.6 release, 2.0 will be a drop-in replacement for any previous version of DataRequestor. I’ll be releasing some new features with that release (foremost: automatically repeated requests for timed site updates), and offering an alternate execution path I’ve been thinking about, but all your current code will work just the way it always has. We’ll see where it goes from there.

Release Notes

Changes in Version 1.6.1

Changes in Version 1.6

Mike West is a web application developer living in Munich, Germany. Professionally programming for the web since 2000, he's available for contract work now a web developer at Yahoo! Germany. Read Mike's bio, or drop him an e-mail.

Recent Articles


Comments

Usage example available from: http://www.intrnet.nl/download/Example_DataRequestor_1.5.zip

Posted By: Tim 12. August 2006, 15:31

Can someone email me a working example of a form? Thanks.

Posted By: Lou 4. September 2006, 17:01

Very easy to use thanks for this ajax library.
But I have one problem :
It doesn’t work when I have different elements in a table in my form… I can get value of input which is out of table but not value of input which is in table?

Posted By: chit 6. September 2006, 17:01

Hi, could you make an explicit example who use all your function ?

I don’t understand hom use your js.

Thanks.

Posted By: Loïc 19. October 2006, 12:55

Replaced code in function used to collect all items in form…

What’s the reason for using childNodes?? I’m rather new to javascript. So if there is any reason please share your knowledge with me. I’m using elements instead is this wrong??
The thing is if you have elements that differs from FORM-elements everything gets wrong with original syntax…

Please comment!!

this.addArgsFromForm = function(formID) {

var theForm = document.getElementById(formID).elements; // Get form method, default to GET var submitMethod = (theForm.getAttribute(‘method’).toLowerCase() == ‘post’)?_POST:_GET; // Get all form elements and use `addArg` to add them to the GET/POST string for (var i=0; i < theForm.length; i++) {
theNode = theForm[i];
switch(theNode.nodeName.toLowerCase()) { case “input”: case “select”: case “textarea”: this.addArg(submitMethod, theNode.id, theNode.value); break; } } }

Posted By: Kjell Brandes 2. December 2006, 16:52

Can you submit arguments to the server side script you are calling?

Posted By: Bill Hecox 18. December 2006, 20:17

I like using this wrapper. It hides some of the sticky details. Thanks for putting it together and making it freely available. One problem I’ve come across is with the onFail. It will get called correctly if there is a server error, but if the network is completely unavailable (e.g. simply unplug the network from your browser’s computer) then it generates a different JS error and the onFail is never called. I’m building an app that must be tolerant of brief server outages, but this use case is not handled correctly. Do you know of a fix for this?

Posted By: J. Delbert Willden 9. February 2007, 18:08

Hrm. I never thought about disconnecting the browser completely to test. :) I’ll take a look and wrap a solution into a new release shortly.

Posted By: Mike West 11. February 2007, 19:10

Regarding Version 1.6: For production purposes, you should remove line 326 with the “console.log()” command as it throws an error in Internet Explorer 7. (This must be used for debugging with Firefox.)

Here’s more information I found via Google researching the issue: http://www.wait-till-i.com/index.php?p=394

http://icant.co.uk/sandbox/fauxconsole/

Posted By: James Moberg 5. April 2007, 00:24

That was hugely stupid of me, James. Thanks for pointing it out. It’s now been corrected.

Posted By: Mike West 16. April 2007, 21:55

Comments Closed

Comments are closed after 6 weeks to avoid the icky comment spam we all loathe. Sorry if you missed the opportunity, but you can always drop me an e-mail!

flickr Photostream

Yahoo!'s Munich Office
atom:title
atom:title
atom:title