0

I m new to jquery nd ajax kindly help me in this code as dis code is not working it is showing complete html code instead of text(hiiiii) A.html

<html>
<head>

<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#btn").click(function(){
    $.post("hello.jsp",function(data,status){
      $("#mydiv").text(data);
    });
  });
});
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" id="btn">Request data</button>
<div id="mydiv"></div>
</body>
</html> 

hello.jsp

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
   <%
    int i=0;
    int j=++i;
    out.println(" hiiiiii" +(j));
    %>
    </body>
    </html>

output ajax a button jsp page's code

2 Answers2

0

just change this

 $("#mydiv").text(data);

to

 $("#mydiv").html(data);

and also check this What is the difference between jQuery: text() and html() ?

Community
  • 1
  • 1
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
0

You are inserting the data with $("#mydiv").text(data);. The manual for that function says:

We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML.

That page also mentions the html() method, which is what you want to insert HTML into a document.

$("#mydiv").text(html);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335