从XMLHttpRequest responseText获取纯文本

Sas*_*sha 5 ajax struts2

任何人都可以告诉我如何从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);
Run Code Online (Sandbox Code Playgroud)

在struts.xml

    <action name="getMessage" class="SampleAction" method="getMessage"/>
Run Code Online (Sandbox Code Playgroud)

行动

    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;
}
Run Code Online (Sandbox Code Playgroud)

当我打印响应文本(使用警报)时,它打印包含所有HTML信息的消息.实际消息以粗体突出显示

回复消息

html> head> title> Apache Tomcat/5.0.28 - 错误报告/标题>样式>! - {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;字体大小: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;} BODY {font-family:Tahoma,Arial,sans-serif;颜色:黑色;背景颜色:白色;} B {font-family:Tahoma ,Arial,sans-serif;颜色:白色;背景颜色:#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 >> HTTP Status 404 - 没有为动作定义的结果com.sample.SampleAction $$ EnhancerByCGLIB $$ 69b4e30e和结果示例消息 HR size ="1"noshade ="noshade"> p> b>类型/ b>状态报告/ p> p> b>消息u>没有为动作com.sample.SampleAction定义结果$$ EnhancerByCGLIB $$ 69b4e30e和结果示例消息/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>

Ser*_*rge 0

plainText属性按原样返回服务器响应,不进行任何转换。因此,如果 url 请求返回 html 格式的页面,您将在获得的字符串值中看到所有标记plainText

如果您希望只显示文本,您的 Web 服务器应用程序应该为您的请求返回纯文本格式的响应