Kevin Haverlock

Subscribe to Kevin Haverlock: eMailAlertsEmail Alerts
Get Kevin Haverlock: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Blog Feed Post

Dealing with XHR failures



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 fromthe 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 fromthe 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 attributesthat 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...

More Stories By 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.