0

I am making a JSP Application that calls Ajax request to the Servlet. I had taken reference from How to use servlets and Ajax.

I am facing a problem in adding responseText as innerHTML of my div. I tried

$('#testdiv').html(responseText);

but it is displaying it as text instead of as HTML.

My Code is:-----------------

function getdata(){     
            var mobile = document.loyaltyForm.mobile.value;
            var code = document.loyaltyForm.mobilecode.value; 
            var response = '';
            var testdiv = document.getElementById("testdiv");
            // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...
            $.post('loyal',  {mob:mobile,mobilecode:code}, function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
                alert('Response:'+responseText);
            });

    }   
Community
  • 1
  • 1
Ankit
  • 2,126
  • 4
  • 33
  • 53
  • 2
    Sorry, but your question is incomprehensible. It's printing the text in the html page because that's what you said you wanted - you said you wanted it in the div. I don't understand the question. – Ariel Aug 09 '11 at 10:20
  • responseText is printing in html but i wants to print responseText on innerHTML of div 'testdiv' – Ankit Aug 09 '11 at 10:25
  • I had also try this: function(responseText) { alert('hiii-->'+responseText); response = responseText; } alert('hello-->'+response); First alert is printing the reponseText but doesnot copy the value to reponse variable. So, second alert is printing only hello.. I hope i explained my comment.. – Ankit Aug 09 '11 at 10:30
  • 1
    Um, innerHTML of div *IS* html, so I still don't understand what you are saying. To your comment, response is a local variable of the function. I can't tell. Copy your entire code into the question and maybe someone can help you from that. – Ariel Aug 09 '11 at 10:38

1 Answers1

0

That will happen when responseText actually contains for example

<b>bold</b> <i>italic</i> 

instead of

<b>bold</b> <i>italic</i>

In other words, that will happen when you send a HTML-escaped string instead of a real HTML string. Make sure that your servlet doesn't send a HTML-escaped string.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555