The i-Technology Media!
Register | Log in
   
 
.NET  ·  AJAX  ·  CLOUD  ·  ECLIPSE  ·  FLEX  ·  OPEN WEB  ·  iPHONE  ·  JAVA  ·  LINUX  ·  OPEN SOURCE  ·  ORACLE  ·  PBDJ  ·  SEARCH  ·  SILVERLIGHT  ·  SOA  ·  VIRTUALIZATION  ·  WEB 2.0  ·  WIRELESS  ·  XML
Comments
Google Wave Invitation Giveaway
By Aditya Banerjee
Timo Hirvonen wrote: I would really appreciate an invitation. Been desperately trying to find one :) timo [dot] hirvonen [at] gmail [dot]com
Nov. 27, 2009 11:13 AM EST
Cloud Expo on Google News
Did you read today's front page stories & breaking news?


2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts

2009 East
GOLD SPONSORS:
CA
Get Your Transactions Under Control: SOA Performance Management
Software AG
Performance Driven Adoption: The Secret to Advancing SOA
Intel
The Evolving SOA Appliance: 3 Game-Changing Innovations
SILVER SPONSOR:
Denodo
Data Mashups: Deliver Your Project Faster with Virtualized Data Services Across Internal & External Sources
POWER PANELS:
The Business Value of Service Orientation
Driving Profitability Through User Experience
Click For 2008 West
Event Webcasts
Live Google News by SYS-CON!
Top Three Links You Must Click On


Dealing with XHR failures

By: Kevin Haverlock
Oct. 27, 2008 10:16 AM

One of the challenges of working in Ajax is dealing with the Asynchronous request and response. The scenario goes something like this: You create a widget that populates a table with information. The information returned from the server is in JSON format. The requests uses an XMLHttpRequest, or in the case of Dojo, a dojo.xhrGet(..) request. For whatever reason, the server fails to return you the data you expected. This may occur for a number of reasons:

  • The server fails and you get back a 500 error code.
  • The server does not recognize the request and issues some html instead of the JSON you were expecting
  • The Session times out, and you are redirected to a form login page to re-authenticate.

For whatever reason, you'll need to respond to the error condition. Listed below is a roughed out framework of the error conditions an how you might respond within Dojo. The key is to use dojo.fromJSON(data) and parse the JSON response. The parse will fail if you receive HTML from a form login or an HTML error page from the server. If there is a problem, catch the exception and go from there. HTTP error condition codes can be caught by the error function. The ioArgs parameter provides additional attributes that you can use to narrow down the problem.


Kevin Haverlock


<script>
        var timer = null;
        // Function that retrieves a JSON object and puts the information
        // into the div with an id of 'json-content'. Notice how we're defining
        // 'handleAs' to be of type 'text'. We we handle the parsing of the JSON
        // data so that we can trap the exception if it occurs.
        function getJson () {
            
            var d = dojo.xhrGet ({
                                  url:      '/webseal/login',
                                  handleAs: 'text', 
                                  load:     loadIntoNode,
                                  error:    errorCondition              
                                 });
        };


          function loadIntoNode(data, ioArgs){              
            try{
                console.log("Read JSON Data ... do something",data);
				
		// read the JSON data that was returned by the service 
                var jsonData = dojo.fromJson(data);

                console.log(jsonData.attribute);
                console.log(jsonData.number);
		
                //do something with the JSON data that you read
		//dojo.byId("json-content").innerHTML = data;

            }catch(e)
            {
                // respond with an error reading JSON.  As an example, if an HTML form login is returned, then dojo.fromJson 
                // will throw an Exception.  Catch the exception and dispaly a login box. At this point, we are assuming the 
                // error condition is because Dojo can't parse the JSON stream. You can fine tune the error condition by 
                // looking at the e.message value
		console.log("error reading json data ",e.message);
                
                // Do something on the error condition, display a login widget, or look at the ioArgs to further narrow
                // the problem down.
                 
          }

          function errorCondition(error,ioArgs){

           console.log("Status",ioArgs.xhr.status);
              
            // retrieve an error message for the HTTP response code.  As an example, if we get a 500 
	    // (Server Error) then take an action.

            switch(ioArgs.xhr.status) {
            case 404: //page not found error
                      break;
            case 500: // server side error
                      break;
            case 407: // proxy authentication
                      break;
            default: // default action
            };

            console.log("HTTP Error code:",ioArgs.xhr.status);
            console.log("Error Condition:",error.responseText);
	    console.log("Error Message:  ",error.message);

            //dojo.byId("json-content").innerHTML = ioArgs.xhr.status;
                       
          };       
        
</script>        

Read the original blog entry...

Published Oct. 27, 2008— Reads 158
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Kevin Haverlock
Kevin Haverlock is an advisory software engineer for IBM's WebSphere Application Server product. He joined IBM in 1995 at Research Triangle Park, NC, where he worked as a developer for the Tivoli division. In 2000 he transferred to the WebSphere Application Server organization and is currently an architect and developer for the WebSphere Application Server Express product.

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers

ADS BY GOOGLE

Breaking Java News
Atlantic Power Corporation Completes Conversion to Traditional Common Share Structure
Sigma Designs, Inc. Schedules Conference Call to Discuss Fiscal Third Quarter Financial Results
Crown Minerals Increases Size of Non-Brokered Private Placement
Petra Petroleum Inc.: News Release
H2O Innovation Amends its Stock Option Plan and Grants Stock Options
Russell Breweries Inc. Reports Positive EBITA in Q1 2010 Fiscal Results
Lingo Media Reports Third Quarter Results
Quaterra Converts Notes and Interest to Shares
GSI Group Calls Shareholders Meeting
Cannasat Therapeutics Reports Results for the Nine Months Ended September 30, 2009

ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

.NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
Open Web Developer's Journal - OPENWEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2
Apache   |   CMS   |   CRM   |   HP   |   Oracle Journal   |   Perl   |   Python   |   Red Hat   |   Ruby on Rails   |   SAP   |   SaaS

SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
SYS-CON EVENTS:   |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  Cloud Computing Conference & Expo  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
 Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.
 
close this window