如何在ajax响应返回的脚本标记中执行javascript

时间:2022-08-25 18:13:18

I'm sending a jquery get request like so:

我发送了一个jquery get请求如下:

$.get($(this).attr("href"), $(this).serialize(), null, "script");

The response I expect to receive will be wrapped in script tags.

我期望收到的响应将被包装在脚本标记中。

I understand the browser doesn't execute the response unless its returned without the script tags. Normally I would remove the tags from the response but in this situation I don't have access to the code running on the remote machine so cannot strip out the tags at the source.

我知道浏览器不会执行响应,除非它返回时没有脚本标签。通常我会从响应中删除标记,但在这种情况下,我无法访问运行在远程机器上的代码,因此无法删除源代码中的标记。

Is there a way I can strip out the script tags from the response client side and execute the javascript?

是否有一种方法可以从响应客户端去掉脚本标记并执行javascript?

7 个解决方案

#1


26  

You should be able to do the following:

你应该能够做到以下几点:

$.get($(this).attr("href"), $(this).serialize(), function(data){
   var script = $(data).text();
   eval(script);
});

#2


3  

Or:

或者:

var myScript = new Function($('script#myscript',responseText).text());
myScript();

#3


2  

If I understand your question right, this should suffice to get the text out of the script tags:

如果我理解了你的问题,这就足以让你从脚本标签中获得文本:

$(response).text()

$(反应)。text()

#4


2  

Would this help you: http://docs.jquery.com/Ajax/jQuery.getScript ?

这对您有帮助吗:http://docs.jquery.com/Ajax/jQuery.getScript ?

#5


1  

Jose Basilio's answer is okay, but I recommend replacing eval with jQuery's globalEval function...

Jose Basilio的答案是可以的,但是我建议用jQuery的globalEval函数替换eval…

$.get($(this).attr("href"), $(this).serialize(), function(data) {
   script = $(data).text();
   $.globalEval(script);
});

globalEval is the function that would normally be called when you call an ajax method with a return type of script.

globalEval是在使用返回类型的脚本调用ajax方法时通常调用的函数。

This from the API documentation...

这来自API文档…

This method behaves differently from using a normal JavaScript eval() in that it's executed within the global context (which is important for loading external scripts dynamically).

该方法的行为与使用普通的JavaScript eval()不同,因为它是在全局上下文中执行的(这对于动态加载外部脚本很重要)。

#6


0  

Say our response is in the 'response' var:

我们的回答是“响应”var:

script = response.replace(/<script>(.*)<\/script>/, "$1"); // Remove tags
eval(script); // Execute javascript

#7


0  

I did this slightly differently, and used php land to make it easier. (I don't like using eval, nor do I like huge conspicuous rewrites).

我的做法稍有不同,并使用php land来简化。(我不喜欢使用eval,也不喜欢显著的重写)。

I placed all my jquery in a php string like so (there was a LOT more JavaScript in real life)

我把所有的jquery都放在一个php字符串中(在现实生活中有很多JavaScript)

    $out .= "       $('#save_now').button(); \n";
    $out .= "       $('#save_now').click( function() {\n";
    $out .= "          return false;\n";
    $out .= "       }); \n";

then also in php land

然后是php

    echo "<script>\n";
    echo "  function onOpen(){ \n";
    echo $out;
    echo "  } \n";
    echo "</script>\n";

then in the jQuery $.ajax call I do this

然后用jQuery $。ajax调用我这样做

   $.ajax({
           url: geturl,
           type: 'post',
           data: getparams,
           success: function(data) {
               mydiv.html(data);  
               onOpen();  
           }, 
           cache: false
      });

as you can see you don't nee the php land thing, it's just in my code base I did sort of need to do it. the trick is to do away with $(document).ready(function(){}); and roll your own

正如你所看到的,你不需要php land的东西,它只是在我的代码库中,我需要它。诀窍是去掉$(document).ready(function(){});滚你自己

#1


26  

You should be able to do the following:

你应该能够做到以下几点:

$.get($(this).attr("href"), $(this).serialize(), function(data){
   var script = $(data).text();
   eval(script);
});

#2


3  

Or:

或者:

var myScript = new Function($('script#myscript',responseText).text());
myScript();

#3


2  

If I understand your question right, this should suffice to get the text out of the script tags:

如果我理解了你的问题,这就足以让你从脚本标签中获得文本:

$(response).text()

$(反应)。text()

#4


2  

Would this help you: http://docs.jquery.com/Ajax/jQuery.getScript ?

这对您有帮助吗:http://docs.jquery.com/Ajax/jQuery.getScript ?

#5


1  

Jose Basilio's answer is okay, but I recommend replacing eval with jQuery's globalEval function...

Jose Basilio的答案是可以的,但是我建议用jQuery的globalEval函数替换eval…

$.get($(this).attr("href"), $(this).serialize(), function(data) {
   script = $(data).text();
   $.globalEval(script);
});

globalEval is the function that would normally be called when you call an ajax method with a return type of script.

globalEval是在使用返回类型的脚本调用ajax方法时通常调用的函数。

This from the API documentation...

这来自API文档…

This method behaves differently from using a normal JavaScript eval() in that it's executed within the global context (which is important for loading external scripts dynamically).

该方法的行为与使用普通的JavaScript eval()不同,因为它是在全局上下文中执行的(这对于动态加载外部脚本很重要)。

#6


0  

Say our response is in the 'response' var:

我们的回答是“响应”var:

script = response.replace(/<script>(.*)<\/script>/, "$1"); // Remove tags
eval(script); // Execute javascript

#7


0  

I did this slightly differently, and used php land to make it easier. (I don't like using eval, nor do I like huge conspicuous rewrites).

我的做法稍有不同,并使用php land来简化。(我不喜欢使用eval,也不喜欢显著的重写)。

I placed all my jquery in a php string like so (there was a LOT more JavaScript in real life)

我把所有的jquery都放在一个php字符串中(在现实生活中有很多JavaScript)

    $out .= "       $('#save_now').button(); \n";
    $out .= "       $('#save_now').click( function() {\n";
    $out .= "          return false;\n";
    $out .= "       }); \n";

then also in php land

然后是php

    echo "<script>\n";
    echo "  function onOpen(){ \n";
    echo $out;
    echo "  } \n";
    echo "</script>\n";

then in the jQuery $.ajax call I do this

然后用jQuery $。ajax调用我这样做

   $.ajax({
           url: geturl,
           type: 'post',
           data: getparams,
           success: function(data) {
               mydiv.html(data);  
               onOpen();  
           }, 
           cache: false
      });

as you can see you don't nee the php land thing, it's just in my code base I did sort of need to do it. the trick is to do away with $(document).ready(function(){}); and roll your own

正如你所看到的,你不需要php land的东西,它只是在我的代码库中,我需要它。诀窍是去掉$(document).ready(function(){});滚你自己