Get text from XMLHttpRequest responseText

Can someone tell me how to extract the string returned by the Struts action class from an AJAX response? The following is a snippet of code:

JS call :

    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"/>

Act

    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;
}

When I print the response text (using a warning), it prints a message with all the HTML information included. the actual message is in bold

reply message

html > head > title > Apache Tomcat/5.0.28 - / > > ! - {font-family: Tahoma, Arial, sans-serif; : , : # 525D76; font-size: 22px;} H2 {font-family: Tahoma, Arial, sans-serif, : , background-color: # 525D76; font-size: 16px;} H3 {font-family: Tahoma, Arial, sans -serif; : , : # 525D76; font-size: 14px;} BODY {font-family: Tahoma, Arial, sans-serif; : ; background-color: white;} B {font- : Tahoma, Arial, sans-serif, : , : # 525D76;} P {font-family: Tahoma, Arial, sans-serif; : ; : ; : 12 ; } 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 > u > com.sample.SampleAction $$ EnhancerByCGLIB $$ 69b4e30e Sample messag e/u > /p > p > b > description/b > u > ( , action com.sample.SampleAction $$ EnhancerByCGLIB $$ 69b4e30e ) . /u > /p > HR size = "1" noshade = "noshade" > h3 > Apache Tomcat/5.0.28/h3 > /body > html >

+5
3

.

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

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

plainText , - . , url html- , , plainText

, -

0

Try:

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

source

0
source

All Articles