html snippet returned by xmlhttprequest.responseText displayed as text

M

mihirnmehta

This is my code

function getDetails()
{
var name = document.getElementById("movie_name").value;

if (window.XMLHttpRequest) //For Mozilla Browsers
{
XMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject) // For Microsoft Browsers
{
XMLHttp=new ActiveXObject('Microsoft.XMLHTTP')
}

var
url="http://localhost/WebService1/Service.asmx/GetMovieByName";
var params="name=" + name;

XMLHttp.open("POST",url,true);

XMLHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XMLHttp.setRequestHeader("Content-Length", params.length)
XMLHttp.onreadystatechange=stateChanged ;

XMLHttp.send(params);
}

function stateChanged()
{
if (XMLHttp.readyState==4 || XMLHttp.readyState=="complete")
{

var v = document.getElementById("responseDiv");
v.innerHTML = XMLHttp.responseText;
}
}

and there a division like

<div id="responseDiv" align=center>
</div>

below in the page.


Now i have checked that my webservice returns the proper html snippet
as string and that the XMLHttp.responseText contains proper html
snippet(a table). But it is displayed as text . The html is correctly
displayed but i want it to show the table rather than the html code?

Can anybody help me out.

Thanks
 
M

Martin Honnen

var v = document.getElementById("responseDiv");
v.innerHTML = XMLHttp.responseText;

Now i have checked that my webservice returns the proper html snippet
as string and that the XMLHttp.responseText contains proper html
snippet(a table).

How did you check that exactly? Which user agents have you tried?
Setting innerHTML parses the string you set to as HTML so I don't think
what you describe is possible. You are either setting innerText (or
textContent) instead of innerHTML or the responseText contains escaped
HTML markup (e.g. &lt;table&gt; instead of <table>).
 
M

mihirnmehta

Thanks Martin, but here's how i did it.


My webservice gets me the data in the form of XML and i parse that XML
on the serverside using XSLT and convert it to HTML and return the html
in a string variable. While Debuggin the webservice i checked that the
HTML is something like <table>.......</table> and not
&lt;table&gt; as you thought it would be.

So when i press the button....the division gets painted by the
following text.............


<table>...
.....
.....
......</table>.

I dont understand whats going on. And i have double checked it taht i
am settig the innerHTML property.

See if you can tell me smth more on this.
Thanks again
 
M

mihirnmehta

Hey Martin,

you were right, I later rechecked that the webservice returns a string
something like.......

<table>......</table>

which is reciebed on the client side as
&lt;table&gt;......&lt;/table&gt;

so it is displayed as text. Can you tell me how to solve this problem?
 
M

mihirnmehta

Hey Martin,

You were right. I put an alert(XMLHttprequest.responseText) and the
alert shows me the html returned by the webservice in the form of
escaped markup as you had said. so its displays the html code. rather
than the table that i expect it to display.

So now what should i do if i want to get the table displayed?

Thanks
 
M

Martin Honnen

You were right. I put an alert(XMLHttprequest.responseText) and the
alert shows me the html returned by the webservice in the form of
escaped markup as you had said. so its displays the html code. rather
than the table that i expect it to display.

So now what should i do if i want to get the table displayed?

Have you considered changing the web service to return XML? Or if you
want to return HTML markup that is not well-formed XML then at least
change the web service to send the HTML as plain text and not escaped.

Other than that you can only do what you have tried e.g.
var div = document.createElement('div');
div.innerHTML = XMLHttp.responseText;
v.innerHTML = div.innerText;
 
M

mihirnmehta

Thanks Martin,

But
I AM returning a html as normal text from the webservice.
something like this........

string str = <b>Hello there!!!!</b> //Actually the html is generated
by xslt from an xml doc
return str;

while debugging i have checked taht the contents of str
are normal text like "<b>Hello there!!!!</b> "

and on the client side there's an javascript alert like

alert(XMLHttprequest.responseText);

which shows some XML headers like <?xml version"1" encoding="UTF8">
etc etc
and then

&lt;b&gt;Hello there!!!&lt;/b&gt;

I'll see if i have some other function with respect to responseXML on
the client side

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top