|
SYS-CON.TV WEBCASTS YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP LINKS YOU MUST CLICK ON AJAX AJAX Custom Error Handling: Enhancing the Interactive User Experience
AJAX and Rich Internet Applications
By: Ryan Anklam
Mar. 19, 2006 09:15 AM
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? This article will guide you through one possible way to implement custom error handling in AJAX using many of the same techniques that you'll likely read about in other 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. YOUR FEEDBACK
ENTERPRISE OPEN SOURCE MAGAZINE LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||