Switch to full style
HTML/DHTML/JAVASCRIPT/AJAX Technology Tutorials Written By Members.
Post a reply

Error handling in AJAX

Thu Aug 23, 2012 1:04 am

In AJAX you use XMLHttpRequest object and waits for readyState, in case of error state is 4:
Code:
   if(request)
   {
   //The autocalled funtion when the status changed
request.onreadystatechange = function() {

//Status cases of the readState varrible
  if(request.readyState==0)
document.getElementById('status').innerHTML='Uninitialized';
else  if(request.readyState==1)
document.getElementById('status').innerHTML='Loading...';
else  if(request.readyState==2)
document.getElementById('status').innerHTML='Loaded';
else  if(request.readyState==3)
document.getElementById('status').innerHTML='Interactive';
else  if(request.readyState==4)
{
document.getElementById('status').innerHTML='Completed';
var xml_response = request.responseXML;
xml_response.loadXML(request.responseText);
    var myname=xml_response.getElementsByTagName('name');
    document.getElementById('Txtdata').value=myname[0].childNodes[0].nodeValue;
}


in case of using DOJO toolkit error code is 500

Code:

dojo
.xhrPost( {
    url: 'service URL',
    content: {},
    handleAs: 'text',
    load: function(response, ioArgs) {
        // Do something
    },
    error: function(response, ioArgs) {
        alert("Failed while doing the operation: " + ioArgs.xhr.response);
    }
});
 





If you are connecting to a web-server which runs a Servelt and wants to return error for ajax code at client side you can set exception as follows :
Code:

public class siteServelt extends HttpServlet 
{

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
        try 
{
             // Your code site here.
        } catch (Exception exception) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    response.getWriter().write(exception.getMessage());
                    response.flushBuffer();            
        
} finally {
             
        
}
    }
    
}
 




Post a reply
  Related Posts  to : Error handling in AJAX
 AJAX SYNTAX ERROR IE     -  
 Solution to Error status zero when using Ajax with JQuery     -  
 What is AJAX, How to start AJAX?     -  
 Exception handling     -  
 What is Event Handling?     -  
 try-with-resource automatic handling     -  
 Event Handling Notes     -  
 ComponentListener event handling     -  
 Session handling using Struts     -  
 Exception Handling in a project     -  

Topic Tags

AJAX