我是XMLHttpRequest
的新手,但已成功使用GET
从服务器更新时钟。
现在,我需要发送POST
和接收XML
。我在服务器上的PHP
正在接收帖子,甚至以XML格式发送回数据,但是如果XML元素包含类似html的代码,我会遇到一个奇怪的问题:responseXML将元素的文本从html代码中剪切到末尾。
以下是相关代码:
PHP发送XML:
header( 'Content-type: text/xml' );
echo '<?xml version="1.0" encoding="UTF-8" ?>';
echo "<posts>";
echo "<textblock>Block with html<br />Next line in block</textblock>";
echo "<inputline>input line</inputline>";
echo "</posts>";
根据Firebug的完整XML响应(注意所有数据都在其中)
<?xml version="1.0" encoding="UTF-8" ?><posts><textblock>Block with html<br />
Next line in block</textblock><inputline>input line</inputline></posts>
回到浏览器中,接收到的JS变量将获取“使用html阻止”,除此之外没有其他。 HttpPostExch是XMLHttpRequest
var received =
HttpPostExch.responseXML.getElementsByTagName("textblock")[0].firstChild.data;
下一个元素inputline可用,没有问题。上面是FF16,IE8也是一样。因此,在
HTML
中包含XML
代码时,我应该做错了。有什么想法为什么当存在html时代码会这样做吗?
最佳答案
您正在访问textblock
节点的第一个子节点,该节点是带有文本"Block with html"
的文本节点。这里的行为是完全可以预期的。如果要整个textblock
节点,请使用:
HttpPostExch.responseXML.getElementsByTagName("textblock")[0].data;