我正在进行Ajax调用,我从服务器收到错误。
现在问题是我收到以下消息。
HTTP Status 756 - Error while processing the request.
--------------------------------------------------------------------------------
type Status report
message Error while processing the request.
description Cannot find message associated with key http.756
我想从完整的错误报告中获取错误消息,而不是上面的所有文本。我怎么能这样做?
但实际的反应是
<html><head><title>Apache Tomcat/5.0.28 - 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 756 - Error while processing the request.</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Error while processing the request.</u></p><p><b>description</b> <u>Cannot find message associam<D‡üñÔE(1@@ähttp.756</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.0.28</h3></body></html>
我想从哪里获取错误消息。
答案 0 :(得分:2)
当您获得response
HTML时,您可以抓住这样的消息......
var div = document.createElement("div");
div.innerHTML = response;
var errorMsg = [].filter.call(div.getElementsByTagName("b"), function(b) {
return b.textContent == "message";
})[0].nextElementSibling.textContent || "Unknown error";
如果只是文字......
这将在第一行-
之后提取文本。如果找不到匹配项,则会返回“未知错误”。
var errorMsg = (response.split("\n")[0].match(/^HTTP Status \d+ - (.+)$/)
|| [])[1]
|| "Unknown error";
相反,如果您希望匹配以下message
行。
var errorMsg = (response.match(/^message (.+)$/m) || [])[1] || "Unknown error";
答案 1 :(得分:1)
请查看此工作示例:Regex
(?<=-\s).*
OR
(?<=[0-9]\s-\s).*
这将获取确切的消息:Error while processing the request.
修改强>
如果它包含HTML
,那么这将有效:Updated Regex
(?<=<h1>).*(?=</h1>)
答案 2 :(得分:0)
我通过以下JavaScript代码得到了答案。
var res = "Error Message : '";
var bonly = data.responseText.match(/<h1>(.*?)<\/h1>/);
if (bonly && (bonly.length > 1)) {
res += bonly[1];
}
res += "'. Error Code : ";
res += data.status;
return res;