任何人都可以請告訴我如何從AJAX響應中提取由Struts操作類返回的字符串?下面是我的代碼片段:從XMLHttpRequest獲取純文本responseText
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>頭>標題>的Apache Tomcat/5.0.28 - 錯誤報告/ TITLE>風格> - {FONT-FAMILY:宋體,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 :font-family:14px;} H3 {font-family:Tahoma,Arial,sans-serif; color:white; background-color:#525D76; font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;顏色:黑色;背景顏色:白色;} 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狀態404 - 沒有爲操作定義結果com.sample.SampleAction $$ EnhancerByCGLIB $$ 69b4e30e和結果示例消息 HR size =「1」noshade = 「noshade」> p> b> type/b>狀態報告/ p> p> b>消息u>沒有針對操作定義的結果com.sample.SampleAction $$ EnhancerByCGLIB $$ 69b4e30e和結果示例消息/u> /請求的資源(沒有針對操作com.sample.SampleAction $$ EnhancerByCGLIB $$ 69b4e30e和結果示例消息)定義的結果不可用./u>/p>HR大小=「1」noshade =「noshade」> h3> Apache Tomcat/5.0.28/h3>/body> html>
請向我們展示一個HTML輸出示例,並指出您想要抓取哪個字符串。然後我們可以爲你寫一些代碼。 – L0j1k