谁能告诉我如何从 AJAX 响应中提取 Struts 动作类返回的字符串?下面是我的代码片段:
JS调用:
xmlhttp=new XMLHttpRequest();
xmlhttp.open('POST', 'getMessage.do', false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send();
alert(xmlhttp.responseText);
Struts.xml
<action name="getMessage" class="SampleAction" method="getMessage"/>
行动
public String getMessage() {
String msg = null;
HttpSession hSession = this.request.getSession(false);
if(null != hSession) {
if(null != hSession.getAttribute("user")) {
User user = (User) hSession.getAttribute("user");
if(null != user.account) {
msg = user.account.getMessage(); //Sample message
}
}
}
return msg;
}
当我打印响应文本(使用警报)时,它会打印包含所有 HTML 信息的消息。实际消息以粗体突出显示
响应消息
html>head>title>Apache Tomcat/5.0.28 - 错误报告/title>style>!-- {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;颜色:白色;背景颜色:#525D76;字体大小:14px;} 正文 {字体系列:Tahoma,Arial,无衬线;颜色:黑色;背景颜色:白色;} B {字体系列:Tahoma ,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {颜色:黑色;}A.name {颜色:黑色;}HR {颜色:#525D76;}-->/style> /head>body>>HTTP 状态 404 - 未为操作 com.sample.SampleAction$$ 定义结果EnhancerByCGLIB$69b4e30e 和结果示例消息HR size="1" noshade="noshade">p>b>type/b> 状态报告/p>p>b>message u>No result defined for action com.sample.SampleAction$$EnhancerByCGLIB$$69b4e30e and result示例消息/u>/p>p>b>description/b> u>请求的资源(未为操作 com.sample.SampleAction$$EnhancerByCGLIB$$69b4e30e 和结果示例消息定义结果)不可用。/u> /p>HR size="1" noshade="noshade">h3>Apache Tomcat/5.0.28/h3>/body>html>