加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – 从XMLHttpRequest responseText获取纯文本

发布时间:2020-12-16 02:56:37 所属栏目:百科 来源:网络整理
导读:任何人都可以告诉我如何从 AJAX响应中提取Struts动作类返回的字符串?以下是我的代码片段: JS电话: xmlhttp=new XMLHttpRequest(); xmlhttp.open('POST','getMessage.do',false); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlenc
任何人都可以告诉我如何从 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 – 错误报告/标题>样式>! – {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:22px;} H2 {font-family:Tahoma,sans-serif; color:white; background-color:#525D76; font-size:16px;} H3 {font-family:Tahoma,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:14px;} BODY {font-family:Tahoma,sans-serif;颜色:黑色;背景颜色:白色;} B {font-family:Tahoma,sans-serif;颜色:白色;背景颜色:#525D76;} P {font-family:Tahoma,sans-serif; background:white; color:black; font-size:12px;} A {颜色:黑色;} A.name {颜色:黑色;} HR {颜色:#525D76;} – > / style> / head> body>> HTTP状态404 – 没有为操作com.sample.SampleAction定义的结果$$EnhancerByCGLIB $$69b4e30e和结果示例messageHR size =“1”noshade =“noshade”> p> b> type / b> ;状态报告/ p> p> b>消息u>没有为动作com.sample.SampleAction定义的结果$$EnhancerByCGLIB $$69b4e30e和结果示例消息/ u> / p> p> b> description / b> u>请求的资源(没有为动作com.sample.SampleAction $$EnhancerByCGLIB $$69b4e30e和结果示例消息定义的结果)不可用./uu0026gt;/pu0026gt;HR size =“1”noshade =“noshade”> h3> Apache Tomcat / 5.0.28 / h3> / body> html>

解决方法

这样做是这样的..

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();

行动

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读