Query about displaying xmlhttp output

H

hatsumoto

Hello,

I create an ActiveXObject("Msxml2.XMLHTTP") from my HTML page to submit
(i.e. post) XML to a server.
I can see the content of the XML response via javascript
alert(xmlhttp.responseText).
Is there a way to display the content of xmlhttp.responseText on a new
page??
I tried document.write(xmlhttp.responseText) but this does not display
the XML structure.

Thanks in advance,
Hats...

------------------------------------
<script>
var xmlhttp=false;
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
function submitXML()
{
xmlhttp.open("POST", "http://myurl...", true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
alert(xmlhttp.responseText);
//document.write(xmlhttp.responseText);
}
}
xmlhttp.setRequestHeader("Content-Type", "text/xml")
xmlhttp.send("xml data...");
}
</script>
 
M

Martin Honnen

I create an ActiveXObject("Msxml2.XMLHTTP") from my HTML page to submit
(i.e. post) XML to a server.
I can see the content of the XML response via javascript
alert(xmlhttp.responseText).
Is there a way to display the content of xmlhttp.responseText on a new
page??

Depends on what kind of display you want. responseText is simply a
string and you can use that any way you use strings to display something
e.g.
var text = document.createTextNode(xmlhttp.responseText);
document.body.appendChild(text);
 
E

Evertjan.

Martin Honnen wrote on 15 mei 2006 in comp.lang.javascript:
Depends on what kind of display you want. responseText is simply a
string and you can use that any way you use strings to display something
e.g.
var text = document.createTextNode(xmlhttp.responseText);
document.body.appendChild(text);

When it contains html elements, you could escape them:

var text =
document.createTextNode(xmlhttp.responseText.replace(/</g,'&lt;'));

and perhaps followed by: .replace(/\n/g,'<br>')
 
M

Martin Honnen

Evertjan. wrote:

When it contains html elements, you could escape them:

var text =
document.createTextNode(xmlhttp.responseText.replace(/</g,'&lt;'));

I can't follow you on this, if you create a text node then there is no
need at all to escape any markup. A text node contains plain text and
the argument to createTextNode is simply interpreted as plain text and
is not parsed by an HTML or XML parser.
 
E

Evertjan.

Martin Honnen wrote on 15 mei 2006 in comp.lang.javascript:
Evertjan. wrote:



I can't follow you on this, if you create a text node then there is no
need at all to escape any markup. A text node contains plain text and
the argument to createTextNode is simply interpreted as plain text and
is not parsed by an HTML or XML parser.

Could very well be. I never tried.
Are returns and multiple spaces shown as is?

Let's try:

<body>
===========<br>zz

<script type='text/javascript'>
var text = document.createTextNode('hello\nworld<br>Hi');
document.body.appendChild(text);
</script>

this shows:

===========
zzhello world<br>Hi
 
M

Martin Honnen

Evertjan. wrote:

this shows:

===========
zzhello world<br>Hi

So why do you think you need to escape the '<' as '&lt;'?

As for the white space, you would need to put the text node into a pre
element if you want white space to be preserved.
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top