How to change responseText of ajax dynamically?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nesha
    New Member
    • Sep 2010
    • 9

    How to change responseText of ajax dynamically?

    Hi,
    i have one jsp page that will generate and print random number. i called that page using ajax. in calling page if i click a link it will call ajax coding. but the responseText is not changing while i click that link until i close the page and reload though i refresh the page. it prints the same number which i got in first ajax call. how to resolve the problem. this is my code
    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib uri="WEB-INF/c.tld" prefix="c" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <script type="text/javascript">
    var xh;
    function greet() {
    	if(window.XMLHttpRequest) {
    		xh = new XMLHttpRequest();
    	} else {
    		xh = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    xh.onreadystatechange = function() {
    		var result = xh.responseText;
    		document.getElementById('greet').innerHTML = result;
    }
    	xh.open("GET","testjsp.jsp",true);
    	xh.send();
    	
    }
    </script>
    </head>
    <body>
    <div id='greet'>Greet</div>
    <a href="javascript:greet()">Click</a>
    </body>
    </html>
    
    And here is my JSP page, testjsp.jsp
    
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%int x = (int) (Math.random() *10); %>
    <%System.out.println(x); %>
    <%=x %>
    <%//test.TestPS()%>
    </body>
    </html>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    this might be a caching issue ... try to add a unique querystring to the url like:

    Code:
    xh.open("GET", "testjsp.jsp" + "?" + (+new Date), true);

    Comment

    • nesha
      New Member
      • Sep 2010
      • 9

      #3
      Hi,
      Thanks for your reply. Its working fine. But what is happening when adding unique query string with url?
      I'm new to ajax, so can u explain me?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        that makes the URL unique so that it cannot be read from the cache and is reliably retrieved again from the server ...

        Comment

        • nesha
          New Member
          • Sep 2010
          • 9

          #5
          Thanks for your reply.

          Comment

          Working...