Javascript jQuery:评估 ajax 响应中的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2158075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:00:32  来源:igfitidea点击:

jQuery: Evaluate script in ajax response

javascriptjqueryxmljson

提问by bobobobo

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

来自我的 web 应用程序的 XML 响应既有要添加到页面的 HTML,也有要运行的脚本。

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

我正在尝试从我的 webapp 发回 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>

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

我现在正在做的是捕捉<html-to-insert><script>CDATA,将 html 插入页面并 eval'ing <script>

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

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

采纳答案by St.Woland

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

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

// 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 ) ;

回答by Cristian Toma

You can use the jQuerylibrary to make the XML request to your backend and also parse it

您可以使用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());
    }
  });
});

You can find out more about jQuery hereand here

您可以在此处此处找到有关 jQuery 的更多信息

I haven't tested it, but it should work according to this article.

我还没有测试过,但它应该可以根据这篇文章工作

回答by IgniteCoders

Thisis the best answer that i found. Work perfect:

是我找到的最好的答案。工作完美:



element.innerHTML = xmlhttp.responseText;
var scriptElements = element.getElementsByTagName('SCRIPT');
for (i = 0; i < scriptElements.length; i ++) {
    var scriptElement = document.createElement('SCRIPT');
    scriptElement.type = 'text/javascript';
    if (!scriptElements[i].src) {
        scriptElement.innerHTML = scriptElements[i].innerHTML;
    } else {
        scriptElement.src = scriptElements[i].src;
    }
    document.head.appendChild(scriptElement);
}


Thanks to Joseph the Dreamer. Original answer here.

感谢梦想家约瑟夫。原始答案在这里

EDIT:

编辑:

Clarification:

澄清:

  1. scripts only run inside scripttag
  2. added scripts after document load, only take effect if it is added to the headtag
  1. 脚本只在script标签内运行
  2. 添加文档加载后的脚本,只有添加到head标签才生效

Thanks to Deniz Porsukfor the comment to improve the answer

感谢Deniz Porsuk的评论以改进答案

回答by code_burgar

JSON would be better suited for this purpose than XML imho.

JSON 比 XML 更适合这个目的。