When the java program does not find the required information I want to return an appropriate error message (could be one of a number of identified reasons). My java code is:
}else{
response.sendError(response.SC_BAD_REQUEST, "Captcha description not correct");
response.getWriter().write("Captcha description incorrect.");
}
And the ajax is:
$.ajax({
type: "POST",
url: "AccountRecoveryQuestionsView",
cache: false,
data: $(questionForm).serialize(),
success: function(data){
$('#ajaxGetUserServletResponse').text(data);
},
error: function(data) {
$("#accountName").focus();
//$('#ajaxGetUserServletResponse').text('An error occured validating the questions.');
$('#ajaxGetUserServletResponse').text(data);
}
});
The error function is triggered; however, the message is not displayed.
I have changed the code to:
statusCode: {
400: function(jqXHR, textStatus, errorThrown){
alert(jqXHR.status);
alert(textStatus);
alert(errorThrown);
$("#accountName").focus();
$('#ajaxGetUserServletResponse').text(errorThrown);
}
},
And the result is 400, error, blank.
I have changed the ajax to:
.fail (function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.status);
alert(textStatus);
alert(errorThrown);
alert(jqXHR.statusText);
alert(jqXHR.responseText);
$("#accountName").focus();
//$('#ajaxGetUserServletResponse').text('An error occured validating the questions.');
//$('#ajaxGetUserServletResponse').text(errorThrown);
$('#ajaxGetUserServletResponse').text(jqXHR.responseText);
});
And alert(jqXHR.responseText); is returning:
<!doctype html><html lang="en"><head><title>HTTP Status [400] – [Bad Request]</title><style type="text/css">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;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status [400] – [Bad Request]</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Captcha description not correct</p><p><b>Description</b> The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).</p><hr class="line" /><h3>Apache Tomcat/8.5.15</h3></body></html>
How do I just get the error message "Captcha description not correct". This is contained in the above.