XML HTTP Requests (CallBacks)

Classic ASP demo files included.
ASP.NET is directly compatible with this - just paste the lines of code into your .NET project!

Recently there has been a bit of interest in a "new" technique for doing XML HTTP requests. Now that all browsers support this, it is a good time to use it. ASP.NET 2.0 will support this natively - ~1 line of code required, but with the attached JavaScript file, we can use it in Classic ASP too.

So what is it?
XML HTTP Requests, (which Microsoft is now calling CallBacks) is where a piece of JavaScript can call a server side (ASP/VBScript) page to obtain a result - without the user knowing. I see it used a lot now in populating the next level in a series of linked drop down lists, rather than pre-loading all combinations into Javascript objects and using more code to pull out the ones we want.

Currently many server side fetches by using Javascript to pop up a little window, run a page, then do a window.opener.document….value = x; to return our value. This is ugly in a few ways:
1. Requires quite a few lines of code
2. User sees a little window popup briefly
3. We can not post the data to the server side, return data can only be dropped back to the opener's vars - if still present - or call an existing function.
4. Or we preload all data in Javascript objects which makes the page very large.

Attached is a sample app which I have put together.
xmlRequest.js is the include library which does the work.
xmlRequestTest.asp is a test page to show it working. This just calls the xmlRequest function and has a function waiting for the server side to call back upon.
xmlRequestServer.asp is a test server side page - this is the bit which does your DB lookups, calcs etc and returns the results (as javascript commands).

This solution only uses a few lines of code and is fast to run (much faster again in .NET). There is no negatives to using it, but read my comments in the files carefully. This callback is asynchronous, so once the call has been made, you should not do any more processing until your results are returned. Have the resulting commands kick off the next stage in your processing - see the demo for this.

Usage

HTTPRequest(strRequestURI);

This command will GET the URI specified and execute (eval) the resulting text.
I have added error handling in case the resulting text is an ASP error or just contains a plain Javascript error.
The request also has caching prevention included.
Supports all 3 methods of calling the XMLRequest object (Internet Explorer 5.5+, Mozilla 1.7+?, Firefox 1.0+, Konqueror, Safari-X and Opera 7.6+)

Credits

Based on techniques by Jim Ley - http://jibbering.com/2002/4/httprequest.html

Hunter Beanland
hunter @ beanland.net.au

http://www.beanland.net.au/programming/