|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
TOP THREE LINKS YOU MUST CLICK ON Messaging
Custom Error Handling Using AJAX
Enhancing the interactive experience
By: Ryan Anklam
Oct. 30, 2006 04:30 PM
Digg This!
Page 1 of 2
next page »
AJAX has become an increasingly popular tool to develop RIAs. With AJAX, as with many new technologies, developers often overlook core application issues such as error handling. While many current AJAX frameworks come with ways to handle errors, the built-in error-handling methods might not be quite what you need, and it's possible that you might not even want to adopt a specific AJAX framework at all. So how do you handle errors in AJAX?
Consider for a moment a hypothetical AJAX application that updates employee information. Users will fill out fields and click on a "Save" button to process the update. In a traditional Web application the user expects to wait a moment while the server updates the record then anticipates another page that displays a confirmation message. This is the typical request/return scenario that our Internet conditioning forces us to accept. Now let's look at the same example using AJAX techniques. The end user still fills out form fields and clicks on the "Save" button but instead of seeing the confirmation message, nothing seems to happen. This can leave the user confused and unsure that his information was saved, yet with AJAX, the database update occurred exactly as expected. Here's how you can implement a messaging and error system in a simple employee information maintenance application that will alert a user as records are updated.
Process at a Glance
Create an Area to Display Status Messages
.error{ Once we have our styles set, we have to put them to use. We'll do this by creating an area in our application to display the status messages returned by the server. This display area can be any type of HTML container such as table, div, or span; we'll use a div. Once the container is created, we'll set the initial style to hidden, as follows: <div id="message" name="message" class="hidden"></div>
Creating the ColdFusion Page to Process the Request. First let's look at an example of processing an update of one of our employee records. Since this is a situation where no data is being returned (because we're not using a SELECT statement), we really only need to deliver either a success or error message to our user. To do this, we'll have to create a variable to hold the XML string then add the XML declaration to it: <cfset xml = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>"> Now that we've created our variable, "xml," we're going to want to do some simple data validation - in this case, to make sure that a valid department ID was passed into the template. If the department ID passed in is not valid, we're going to want to set the first node of the XML document to (<error>), add the error message, and close the error node (</error>). For our purposes, we'll assume that a "departmentID" value of 0 or of a non-numeric value constitutes an invalid condition. We're also going to include "try/catch" conditions to cover database errors and general failures:
<cfif departmentId eq 0 OR NOT IsNumeric(departmentId)> One thing to note here is that the entire XML string is overwritten in the catch statement. This eliminates a situation caused by an error being thrown in the middle of building the XML string. Specifically this condition sends an incomplete and unpredictable XML document back to the requesting page.Let's walk through an example of a successful and unsuccessful update of an employee record. The main screen of the employee update application is shown in Figure 1. For this example let's say that a phone extension can only be used once per employee in a company. When a user clicks on the "Update Employee" link the main screen will prepare the AJAX request and send it to a ColdFusion template. The template will then process the update and send a XML message back to the main screen. If everything in the update goes okay the ColdFusion template will send the page a success XML message that would look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><data> Now, let's say that the extension is in use. Our ColdFusion template will return a error XML message that would look something like:
<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?><data> To complete the application we have to have a way to handle the XML sent back to the requesting page.
Creating JavaScript to Handle the Errors We'll start writing this function by setting a variable with a value that will represent the name of the message area - this, again, is our display container. Now that we have our display container we have to set the class of the display container conditionally based on the error parameter. Finally, we can set the innerHTML property of message node equal to the message's value:
function ShowMessage(message, isError) Page 1 of 2 next page »
LATEST AJAXWORLD STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||