gpt4 book ai didi

ajax - 从 XMLHttpRequest responseText 获取纯文本

转载 作者:行者123 更新时间:2023-12-04 21:20:32 25 4
gpt4 key购买 nike

谁能告诉我如何从 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; 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>>HTTP Status 404 - 没有为 Action com.sample.SampleAction$$ 定义结果EnhancerByCGLIB$69b4e30e 和结果 示例消息 HR size="1"noshade="noshade">p>b>type/b> 状态报告/p>p>b>message 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>

最佳答案

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

AJAX 调用

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>

关于ajax - 从 XMLHttpRequest responseText 获取纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12642525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com