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 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:
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>        

© 2008 SYS-CON Media