2012-09-28 86 views
5

任何人都可以請告訴我如何從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>

+0

請向我們展示一個HTML輸出示例,並指出您想要抓取哪個字符串。然後我們可以爲你寫一些代碼。 – L0j1k

回答

0

plainText屬性將原樣返回服務器響應,不進行任何轉換。因此,如果url請求應返回html格式的頁面,您將看到字符串值中的所有標記plainText

如果您希望只有文本,那麼您的Web服務器應用程序應以純文本形式返回響應爲您的請求格式

+0

如果你想只有文本,那麼你的web服務器應用程序應該以你的請求以純文本格式返回一個響應 我該怎麼做而不是'return'示例消息「;'? – Sasha

+0

test_plain_text.php的全部內容:<?php echo'Hello,World!'; ?> – Serge

0

嘗試:

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

source

+0

感謝您的時間。您可以檢查最近添加的響應消息並回復解決方案。我已經嘗試過,但沒有按預期工作。 – Sasha

2

做到這一點是這樣的方式..

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

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> 
+0

+1但是,這裏假設你在其他地方有一個getXmlHttpRequestObject函數,它可以處理早期IE執行XMLHTTPRequest和其他人執行XMLHTTPRequest並返回可用實例的方式之間的差異。 AJAX中的第一個'A'代表異步。通過使用false,你創建了一個同步請求,當你離開你的開發機器時,這個請求可能有點慢。 – Dawn

+0

是啊,我無意中複製了我的JS調用xmlHttpRequest ..我現在編輯了我的代碼.. –