Brent Kearney

Posted on: April 26th, 2006 @ 18:18

In Part 2, I introduced the dojo.io.bind() function. In this breif post, I’ll expand on it slightly with a couple of remarks.

Last time, I sent data to my PHP script by putting all of my data in the URI:


var myUrl = 'myscript.php?var1=foo&var2=bar';

There is a prettier way of doing this. You can use the content parameter to send your data using GET, like so:


dojo.io.bind(
	url: 'myscript.php',
	content: { var1: foo, var2: bar },
	error: errorFunction,
	load: resultFunction
);

As you can see, content takes a string of arguments enclosed in curly brackets, the arguments separated by commas. The arguments are key: value pairs, separated by colons.

The second remark I wanted to make about dojo.io.bind() was in regards to error handling. In the above example, I delegate error handling to a function named “errorFunction()”. This function is automatically sent 2 parameters: type, and errorObject.

The “type” variable always contains “error”. The manual has this to say about errorObject: The errorObject is transport specific, but should provide details about the type and details of the bind failure. Should indeed. If you are lucky, you will be able to access an informative error message by accessing errorObject’s “message” method, like so:


errorFunction(type, errorObject)
{
   alert("io.bind() failed: " + errorObject.message);
}

In my experience so far, the error messages can be less than informative. For example, I received the generic message:


"XMLHttpTransport Error: undefined undefined".

I’m told that there are 3 parts to the errorObjects.message, and the latter two are presently unused. In every case that I got this error, there turned out to be a problem with the PHP script itself. I debugged the problem by turning on error logging for the script, watching the server logs, and by adding a PHP function that emailed me debugging information when the script executes.

Sometimes however, errorObject.message gave me some very useful output for debugging. Be careful what you do with these messages though — you may not want your web visitors to see what it spits out!

Add comment