Hi All,

I am trying to display the helpful message to the user when error occur.

a) I have exception catching in Java as follow

AgencyDAO a = new AgencyDAO();
        try {
            a.updateAgency(agencyId, newAgencyCode, newAgencyName);
        } catch (DAORuntimeException e) {
           String message = "My Custom message";
            request.setAttribute("GROUPING_EXCEPTION", new Exception(message, e));
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);            
        }
        return null;

b) AJAX call from JSP is as follow

$.ajax({
        url: "updateAgency.do",
        data: {
            id: nRow.id, 
            new_code: jqInputs[0].value, 
            new_name: jqInputs[1].value
        },
        success: function() {
            alert("Successfully updated.");
            oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
            oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);                        
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert("Update failed.");
            restoreRow(oTable, nRow);            
        }        
    })

So in case of update fail I am getting message "Update failed." from alert. I want to show "My Custom message".

In Firebug when I check the response I am getting

<html><head><title>Apache Tomcat/6.0.20 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - My Custom message</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>My Custom message</u></p><p><b>description</b> <u>The server encountered an internal error (My Custom message) that prevented it from fulfilling this request.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.20</h3></body></html>

Is there any method ow way I can get that custom message when AJAX call fails?

Thanks
Anjib

Anjibman,

This may not be exactly what you want but should get you started.

First put the report HTML in a div wrapper and convert it to a template with spans in each place you want to insert text, like this:

<div id="http_report">
	<h1>HTTP Status: <span class="status"></span> - <span class="message"></span></h1>
	<hr size="1" noshade="noshade">
	<p><b>type:</b> <span class="type"></span></p>
	<p><b>message:</b> <u><span class="message"></span></u></p>
	<p><b>description:</b> <span class="description"></span></p>
	<hr size="1" noshade="noshade">
	<h3>Apache Tomcat/6.0.20</h3>
</div>

Now for the javascript:

function updateAgency(nRow){
	var url = "updateAgency.do";
	var data = {
		id: nRow.id, 
		new_code: jqInputs[0].value, 
		new_name: jqInputs[1].value
	};
	$.ajax({
		url: url,
		data: data,
		$http_report = $("http_report").hide();
		success: function(data, textStatus, xhr) {
			$http_report.find(".status").html(xhr.status).end().find(".message").html(textStatus).end().find(".type").html(url + ": - success").end().find(".description").html('nRow.id:' + nRow.id + ' - Successfully updated').end().show();
			oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
			oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
		},
		error: function(xhr, textStatus, thrownError) {
			var desc = "<u>The server encountered an internal error (" + thrownError + ") that prevented it from fulfilling this request.</u>";
			$http_report.find(".status").html(xhr.status).end().find(".message").html(thrownError).end().find(".type").html(url + ": - error").end().find(".description").html(desc).end().show();
			restoreRow(oTable, nRow);
		}
	});
}

(untested)

As you will see, I have made an assumption about the function wrapper and the parameter passed to it.

I don't know how to do the "Apache Tomcat ... " bit dynamically so I have left it hard-coded.

Airshow

The response I am getting/capture on the Firefox is auto generated. I don't have any error page define. I am not displaying any new page for the action result.
I do edit operation in the page and display the result in the pop up box.

Then there's a lot of stuff you didn't put in your first post.

Airshow

Sorry for that but does that explain the problem now.

My code assumes that a popup div in the main window, not a popup window.

Popup divs are much easier to work with because you don't need to worry about window-window communication; all DOM elements and all javascript are in the same document, and therefore in the same global scope.

You probably just need some CSS to make my code work:

#http_report {
  position: absolute;
  left: 50px;
  top: 50px;
  padding: 5px;
  border: 2px solid #000;
}

With a bit more effort you can display the popup div in a modal "lightbox". Several jQuery plugins are available for this, eg. my favourite, simplemodal by Eric Martin. This is an attractive solution, which also gives you a close button.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.