jQuery:评估ajax响应中的脚本 [英] jQuery: Evaluate script in ajax response

查看:184
本文介绍了jQuery:评估ajax响应中的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自我的webapp的XML响应都有要添加到页面的HTML,有些还有要运行的脚本。

XML responses from my webapp have both HTML to add to the page AND some have a script to run.

我正在尝试从我的webapp发回XML喜欢:

I'm trying to send back XML from my webapp like:

<?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>

What I'm doing now is snapping out the <html-to-insert> and <script> CDATA, inserting the html into the page and eval'ing <script>.

我正在寻找对我的方法的批评。任何人的建议?

I'm looking for criticism on my approach. Any suggestions from anyone?

推荐答案

你宁愿发送JSON,它更容易解释。示例:

You'd rather send JSON, it's way easier to interpret. Example:

// Suppose your response is a string:
// { html: "<p>add me to the page</p>, script:"alert('execute me');" }
var obj = eval( "(" + response + ")" ) ;
eval( obj.script ) ;

这篇关于jQuery:评估ajax响应中的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆