从AJAX Success解析字符串以作为JQuery运行 [英] Parse string from AJAX Success to run as JQuery

查看:147
本文介绍了从AJAX Success解析字符串以作为JQuery运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的HTML页面上,我有一个AJAX帖子来获取一些数据.返回的数据包含一个字符串,该字符串的内容为原始javascript.

On my HTML page, I have an AJAX Post to get some data. The returned data contains a string, and the content of this string is raw javascript.

$.ajax({
    url: '@Url.Action("GetPropertyDataForInstance", "Local_Data")',
    type: 'POST',
    dataType: 'json',
    data: instancePropertyRequest,
    contentType: 'application/json; charset=utf-8',
    success: function (response) {

        var javscriptRawString = response.javascriptToExecute;   
        var alertString = response.data;
    }
})

javscriptRawString的内容:

The content of javscriptRawString:

alert(alertString);

获得此javascriptRawString之后,该怎么做才能直接在其中执行javascript?

After I get this javascriptRawString, what do I do so that I can directly execute the javascript inside??

推荐答案

由于eval是邪恶的,因此它比eval略好;)

this is slightly better than eval as eval is evil ;)

(new Function(response.data))()

使用eval是邪恶的,因为可能有很多安全漏洞.您正在全局范围内执行代码. Function通过在其自己的范围内执行来实现此目的.

Using eval is evil because there can be lots of security holes. You are executing code in global scope. Function takes of this differently by executing in its own scope.

new Function也更快

以您的情况

$.ajax({
    url: '@Url.Action("GetPropertyDataForInstance", "Local_Data")',
    type: 'POST',
    dataType: 'json',
    data: instancePropertyRequest,
    contentType: 'application/json; charset=utf-8',
    success: function (response) {

        (new Function(response.data))()   

    }
})

new Function从原始文本创建新功能.

new Function creates a new function from a raw text.

()()立即执行功能

如果您使用ajax获得功能,我还将添加一些额外的检查和标头. 检查一下.

i would also add some extra checks and headers if you get your functions with ajax. check this.

https://stackoverflow.com/a/17468822/2450730

编辑

如果要从ajax传递参数

if you want to pass params from ajax

//raw ajax response
var x='param';
alert(x)

如果您想从ajax内部传递参数(不好).

if you want to pass params from inside the ajax (not good.)

success: function (response) {
 var x='param';
 (new Function(response.data))(x)   
}

原始

alert(x);

修改2

如果您得到一个带有 1.脚本 2.参数

if you get a object with 1.the script 2.the params

然后,您需要定义一个参数名称.在本例中为'x'.

then you need to define a argument name.in this case 'x'.

js

 (new Function('x',response.script))(response.param)

RAW response.script

RAW response.script

 alert(x)

更多参数:

(new Function('a','b','c','alert(a+b+c)'))(1,2,3) // 123

测试 ... ... http://jsfiddle.net/pLQzd/ 阅读 ... ... https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function 滚动到底部.

TEST IT ... http://jsfiddle.net/pLQzd/ READ IT ... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function scroll to the bottom.

但是当您从同一ajax调用中获得包含函数和vars的对象时

but as you get the object containing the function and the vars... from the same ajax call

我只会创建已经描述过的东西.

i would simply create something already described.

RAW

var param='hello';
alert(param);

或更简单的

alert('hello');

这篇关于从AJAX Success解析字符串以作为JQuery运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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