5

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 ステータス 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>/p>p>b>説明/b> u>要求されたリソース (アクション com.sample.SampleAction$$EnhancerByCGLIB$$69b4e30e および結果のサンプル メッセージに対して結果が定義されていません) は利用できません。/u> /p>HR size="1" noshade="noshade">h3>Apache Tomcat/5.0.28/h3>/body>html>

4

3 に答える 3

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

アクション

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>
于 2012-09-28T15:28:18.810 に答える
0

このplainTextプロパティは、サーバーの応答を変換せずにそのまま返します。したがって、URL リクエストが html 形式のページを返す必要がある場合は、取得した文字列値にすべてのマークアップが表示されます。plainText

そこにテキストのみを表示したい場合、Web サーバー アプリケーションは、要求に対してプレーン テキスト形式で応答を返す必要があります。

于 2012-09-28T15:06:04.070 に答える
0

試す:

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

ソース

于 2012-09-28T15:17:48.803 に答える