来 self 的网络应用程序的 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>
和 <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和 here
我还没有测试过,但它应该根据 this article 工作.
关于javascript - jQuery:评估 ajax 响应中的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158075/