|
SYS-CON.TV Webcasts
Comments
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
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" ?> 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"" ?> 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) After the ShowMessage function is written we have to write the logic to check the value of the first node returned. Since we called an update page we expect the first node to be named either "error" or "success" so we create code that will check to see if this is true: if(response.childNodes[0].nodeName == 'error') Let's look at our example of updating the employee record again. In the first case we got an XML message that looked like: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> In this case our script would call the ShowMessage function with the error Boolean value set to false. The resulting page would look something like Figure 2. Now let's look at the same page if the server returned an error because the employee's extension is used by another employee. As mentioned above, the XML message returned from the ColdFusion template would look like: <?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?> In this case our script would call the ShowMessage function with the error Boolean value set to true. The resulting page would look something like Figure 3. Coupling with Client-Side and Template Error Checking Let's look at how to integrate traditional ColdFusion error handling into our messaging system first. We'll start by creating two session variables: one to hold a Boolean value to determine if an error has occurred and a second one that will be a string to hold the error message that we're going to return to the visitor. For this example I'll name them session.error and session.errorMessage. The next step is to wrap all your ColdFusion logic in the <cftry> and <cfcatch> statements. In your <cfcatch> statement you'll set the session.error to true and the session.errorMessage equal to the cfcatch.Message. try Finally we'll have to add some logic to control the visibility of our message area. If the session.error message is false the display area is created exactly as before. However, if an error is found we'll set the default style of the message area to "error" and display the error message: <cfif NOT session.error> Reader Feedback: Page 1 of 1
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
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||