| By Kevin Haverlock | Article Rating: |
|
| October 27, 2008 10:16 AM EDT | Reads: |
168 |
- 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 October 27, 2008 Reads 168
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
- Profiles for WebSphere Application Server 6.0
- Ajax Proxy for Web 2.0 Feature pack - quick look
- New Version of the Web 2.0 Feature Pack for Websphere
- Dojo debugging and editors
- Dealing with XHR failures
- Using Google Web Toolkit with WebSphere Application Server
- Ajax Proxy for Web 2.0 Feature pack - quick look
- Dojo debugging and editors
- Getting your arms around the Web 2.0 Feature Pack
- New Version of the Web 2.0 Feature Pack for Websphere
- Contributing blogger - Jared Jurkiewicz
- Adobe vs Dojo


























