Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get plain text from XMLHttpRequest responseText

Tags:

ajax

struts2

Can anyone please tell me how to extract a String returned by Struts action class from AJAX reponse? Below is my code snippet:

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

Action

    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 alert), it printed message with all HTML information included. actual message is highlighted in bold

response message

html>head>title>Apache Tomcat/5.0.28 - Error report/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 - No result defined for action com.sample.SampleAction$$EnhancerByCGLIB$$69b4e30e and result Sample messageHR size="1" noshade="noshade">p>b>type/b> Status report/p>p>b>message u>No result defined for action com.sample.SampleAction$$EnhancerByCGLIB$$69b4e30e and result Sample message/u>/p>p>b>description/b> u>The requested resource (No result defined for action com.sample.SampleAction$$EnhancerByCGLIB$$69b4e30e and result Sample message) is not available./u>/p>HR size="1" noshade="noshade">h3>Apache Tomcat/5.0.28/h3>/body>html>

like image 626
Sasha Avatar asked Nov 04 '22 15:11

Sasha


1 Answers

The way to do it is like this ..

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

ACTION DEFINED IN 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>
like image 113
Ashish Gupta Avatar answered Nov 28 '22 00:11

Ashish Gupta