来自我的 web 应用程序的 XML 响应既有要添加到页面的 HTML,也有要运行的脚本。
我正在尝试从我的网络应用程序发回 XML,例如:
<?xml version="1.0"?>
<doc>
<html-to-insert>
<![CDATA[<p>add me to the page</p>]]>
</html-to-insert>
<script>
<![CDATA[ alert('execute me'); ]]>
</script>
</doc>
我现在正在做的就是把<html-to-insert>
and <script>
CDATA,将 html 插入页面并进行评估<script>
.
我正在寻求对我的方法的批评。有人有什么建议吗?
您可以使用jQuery向后端发出 XML 请求并解析它的库
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "your/url/that/returns/xml",
dataType: "xml",
success: function (xml) {
// xml contains the returned xml from the backend
$("body").append($(xml).find("html-to-insert").eq(0));
eval($(xml).find("script").text());
}
});
});
您可以了解有关 jQuery 的更多信息here http://docs.jquery.com/Traversing and here http://docs.jquery.com/Ajax
我没有测试过,但它应该按照本文 http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)