JQuery Ajax error handling, show custom exception messages

Posted on Stack Overflow See other posts from Stack Overflow
Published on 2008-12-18T12:06:18Z Indexed on 2010/05/10 7:24 UTC
Read the original article Hit count: 704

Filed under:
|

Hey Folks,

I am wondering if there is some way where I can show custom exception messages as an alert in my Jquery ajax error message.

For example, say if I want to throw an exception in server side via Struts by "throw new ApplicationException("User name already exists");", I want to catch this message(User name already exists) in Jquery ajax error message.

jQuery("#save").click(function(){
    if(jQuery('#form').jVal()){
            jQuery.ajax({
                type: "POST",
                url: "saveuser.do",
                dataType:"html",
                data:"userId="+encodeURIComponent(trim(document.forms[0].userId.value)),
                success:function(response){
                    jQuery("#usergrid").trigger("reloadGrid");
                    clear();
                    alert("Details saved successfully!!!");
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                }    
            });
        }
    }
);

On the second alert where I alert thrown error, I am getting undefined and the status code is 500.

I am not sure where I am going wrong. Please let me know on this.

Thanks, Dukes

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about custom-exceptions