从XMLHttpRequest responseText获取纯文本

时间:2012-09-28 15:01:14

标签: 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);

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&gt; head&gt; title&gt; Apache Tomcat / 5.0.28 - 错误报告/标题&gt;样式&gt;! - {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#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-家庭:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;} P {font-family:Tahoma,Arial,sans-serif;背景:白色;颜色:黑色;字体大小:12px; } {color:black;} A.name {color:black;} HR {color:#525D76;} - &gt; / style&gt; / head&gt; body&gt;&gt; HTTP状态404 - 没有为操作com.sample.SampleAction定义的结果$$ EnhancerByCGLIB $$ 69b4e30e和结果示例消息 HR size =“1”noshade =“noshade”&gt p为H. b取代;类型/ b个状态报告/ p&gt; p&gt; b&gt;消息u&gt;没有为操作com.sample.SampleAction定义的结果$$ EnhancerByCGLIB $$ 69b4e30e和结果示例消息 / u&gt; / p&gt; p&gt; b&gt; description / b个u&gt;请求的资源(没有为动作com.sample.SampleAction $$ EnhancerByCGLIB $$ 69b4e30e和结果示例消息定义的结果)不可用./u>/p>HR size =“1” noshade =“noshade”&gt; h3&gt; Apache Tomcat / 5.0.28 / h3&gt; / body&gt; html&gt;

3 个答案:

答案 0 :(得分:2)

这样做的方法就是这样..

AJAX CALL

var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
 alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST", "URL");
xmlhttp.send();

<强> ACTION

public String execute() throws Exception {
      try{
            PrintWriter outWriter = null;
            StringBuffer msg= new StringBuffer("");
            HttpServletResponse httpResponse = ServletActionContext.getResponse();
            try {
                outWriter = httpResponse.getWriter();
                                        msg.append("String to be sent to View");
                    }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{

                if(outWriter!=null){
                    httpResponse.setContentType("text/html");
                    outWriter.println(msg.toString());
                    outWriter.flush();
                    outWriter.close();
                }
            }

        }catch (Exception e) {
            throw new Exception(e);
        }
        return null;
        }

在STRUTS.XML中定义的行动

<action name="MYActionName" class="MYActionNameBean"    method="execute">
            <result type="stream">
                    <param name="contentType">text/html</param>
                    <param name="inputName">inputStream</param>
            </result>
        </action>

答案 1 :(得分:0)

plainText属性按原样返回服务器响应,没有任何转换。因此,如果url请求应返回html格式的页面,您将看到plainText

获得的字符串值中的所有标记

如果您希望只有文本,那么您的Web服务器应用程序应该以纯文本格式返回您的请求的响应

答案 2 :(得分:0)

尝试:

var OriginalString = xmlhttp.responseText;
var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,"");
alert(StrippedString);

source