如何执行AJAX输出脚本脚本、AJAX

2023-09-10 14:30:05 作者:小邋遢

我收到一个AJAX成功输出数据。

I'm getting an ajax output success data.

当数据包含一些HTML文本和脚本。

Where the data contains some html text and a script.

但剧本没有执行,我怎么能执行脚本。

But the script is not executing, how can I execute the script.

比方说,Ajax响应obj是

Let's say Ajax response obj is

<div>something....</div><script>alert("test");</script>

以上code是我的Ajax response.The格是越来越呈现,但警报是行不通的。

the above code is my Ajax response.The div is getting rendered, but the alert is not working.

推荐答案

如果您检索从AJAX调用JSON格式的结果,你可以使用 EVAL 以执行JavaScript。

If you are retrieving the JSON formatted result from AJAX call, you can just use eval to execute the javascript.

假设,如果结果JSON是形成这样的

Assume, if the result json is formed like this

   var res =  '{"Data": "<something>",
              "script": "alert(something)"}';

   var out = eval("(" + res + ")");
   var data = out.data;
   eval(out.script);