Newsletter

Ajax Server Response, responseText and responseXML

Ajax » on Aug 26, 2011 { 5 Comments } By Sivateja

Once we send the request to the destination [ server ], we will get the response from the server in two formats either in Text or XML, i mean we can get the response from the server by using responseText and responseXML propertys of our XMLHttpRequest object

Actually..

responseText — for getting the response in the format of text
responseXML — for getting the response in the format of XML

You can see this in the first example

Or check this

<html>
<head>
<script type="text/javascript">
function fun1()
{
var a;

    if (window.XMLHttpRequest)
    {// If the browser if IE7+[or]Firefox[or]Chrome[or]Opera[or]Safari
      a=new XMLHttpRequest();
    }
   else
    {//If browser is IE6, IE5
      a=new ActiveXObject("Microsoft.XMLHTTP");
    }

a.onreadystatechange=function()
{
  if (a.readyState==4 && a.status==200)
  {
    document.getElementById("myDiv").innerHTML=a.responseText;
   }
}

a.open("POST","my.txt",true);
a.send();
} // fun1() close
</script>
</head>
<body>

<div id="myDiv" style="width: 300px; height: 30px;">
Click on the button below
</div>
<button type="button" onclick="fun1()">Change Content</button>

</body>
</html>

Explanation

  • see we are getting the response in the form of text [ see line number 21 ]
  • once the response is ready, our div content will be replaced with the response text [ see line number 32 ]

Will see more while discussing Ajax with Servlets

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 Responses to “Ajax Server Response, responseText and responseXML”
  1. Leonard Pendon says:

    Really fantastic visual appeal on this website , I’d value it 10 10.

  2. Asif says:

    sir at first line of explation is two “are”.u can correct it.and love ur site very much.

  3. Java4s says:

    @Asif – I did the correction, thank you 🙂

  4. priyanka says:

    can we put a pdf into the same jsp file using the ajax like here you have done for text file….

  5. ANU says:

    VEARY UESFULL THANKSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.