当java程序找不到所需的信息时,我想返回一个相应的错误信息(可能是一些已确定的原因之一)。我的java代码是:
function isValidString(str1) {
return str1 != null && typeof str1 === "string" && str1.length > 0;
}
而ajax是:
}else{
response.sendError(response.SC_BAD_REQUEST, "Captcha description not correct");
response.getWriter().write("Captcha description incorrect.");
}
触发错误功能;但是,不显示该消息。
我已将代码更改为:
$.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);
}
});
结果是400,错误,空白。
我已将ajax更改为:
statusCode: {
400: function(jqXHR, textStatus, errorThrown){
alert(jqXHR.status);
alert(textStatus);
alert(errorThrown);
$("#accountName").focus();
$('#ajaxGetUserServletResponse').text(errorThrown);
}
},
并警告(jqXHR.responseText);正在回归:
.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);
});
如何收到错误消息“Captcha description not correct”。这包含在上面。
答案 0 :(得分:1)
在调用 HttpServletResponse
之后,#sendError
对象不应写入,如上所述
如果响应已经提交,则此方法抛出一个 IllegalStateException异常。使用此方法后,响应应该是 被认为是承诺,不应写入。
因此:
response.sendError(...);
response.getWriter().write(...); // Should not be done
您应该使用错误代码的静态字段访问权限。在您的代码中,当您使用response.SC_BAD_REQUEST
时,可以使用HttpServletResponse.SC_BAD_REQUEST
。自己编写int代码,例如sendError(400, ...)
,不会改变结果。
jQuery中对.error
- 函数的$.ajax
调用已从版本3.0开始删除,因此请勿使用它。相反,您应该使用.done
,.fail
和.always
。 .done
相当于.success
,而.fail
相当于.error
,而.always
有点像finally
- 块中的$.ajax
try-catch(顾名思义,总是运行,无论如何)。
jqXHR
始终返回jQuery XMLHttpRequest对象$.ajax
。因此,您可以直接在var jqxhrobj = $.ajax({
type: "POST",
url: "AccountRecoveryQuestionsView",
cache: false,
data: $(questionForm).serialize()
})
.done(function(jqXHR, textStatus, errorThrown) {
// Success here
$('#ajaxGetUserServletResponse').text(jqXHR.responseText);
})
.fail(function(jqXHR, textStatus, errorThrown) {
// Fail, do something about the error
// Since you have several error codes you want to handle
// you could for instance use a switch case
switch(jqXHR.status) {
case "400":
// It might be an int, or even "400 Bad request",
// change accordingly
// Do something to handle the error
break;
default:
// Handle errors not specified with a case
break;
}
});
// Later in the js code
jqxhrobj.always(function() {
// Execute something here, regardless of success/failure of the $.ajax-call
});
- 调用上运行方法调用,例如
jqXHR
有关<body>
<h1>HTTP Status [errorCode] – [errorCode description]</h1>
<hr class="line" />
<p><b>Type</b> Status Report</p>
<p><b>Message</b> [message passed as 2nd argument in the sendError-call]</p>
<p><b>Description</b> [full errorCode description]</p>
<hr class="line" />
<h3>Apache Tomcat/8.5.15</h3>
</body>
对象的更多信息,请参阅here。
responseText将包含一个完全格式化的html输出,包括head,body等。如果你看一下responseText的主体,它看起来像这样:
.find
您可以在jqXHR.responseText上使用substr
(请参阅here) - 找到正文内部的p标签。你应该返回3个元素;消息将在第二个结果元素内。你可能想要做某种<b>Message</b>
,以摆脱var message = jqXHR.responseText.find("p:eq(1)").text();
// text() will not return the <b>-tags, just "Message [msg passed as argument]"
// We'll just substr from the end of "Message"
message = message.substr(7));
// Remove any leading/trailing whitespace
message = message.trim();
// message now contains the error message from #sendError
alert(message);
。
一种简单但天真的方法,依赖于总是的输出具有完全相同的结构:
c: > npm install -g @angular/cli
答案 1 :(得分:0)
尝试刷新并关闭作家
...
response.getWriter().flush();
response.getWriter().close();
如果它没有帮助,请让sendError
像这样调用最后一个
response.getWriter().write("Captcha description incorrect.");
response.sendError(response.SC_BAD_REQUEST, "Captcha description not correct");
答案 2 :(得分:0)
您可以返回以下内容
Response.status(500).entity("Captcha description incorrect. ").build();
根据您的要求修改错误代码,