简体   繁体   English

执行AJAX返回的脚本

[英]Execute scripts AJAX returns

TIP: "you could simple send an json object / array from php to js, and execute every entry like "update_match('1');" using the eval() function, please stop sending js code that way"- Lucian Depold. 提示:“您可以简单地从php向js发送一个json对象/数组,并使用eval()函数执行每个条目,例如“ update_match('1');”,请停止以这种方式发送js代码”-Lucian Depold。


In the index.php, I have this code, which executes when the document is ready: 在index.php中,我有以下代码,该代码在文档准备就绪时执行:

$.post('php/main.php', {elements: 1}, function(return_msg) {
    alert(return_msg);
});

The respond I get is a bunch of scripts, as expected, with the expected values. 我得到的响应是一堆脚本,具有预期的值。 However, they do not execute! 但是,它们不执行! How to make them execute? 如何使它们执行?

Here is the response: 这是响应:

<script>jsfunction(37069);</script><script>updateTextbox('');</script><script>update_match('1', '19.30 Friday 15/5', '1');</script><script>update_player('1', '1', 'recoba', 'cmf', 'teo', '0', '0');</script><script>update_player('1', '2', 'nesta', 'cb', 'tsoulou', '0', '0');</script><script>update_player('1', '3', 'raul', 'cf', 'striker', '0', '0');</script><script>update_player('1', '4', 'samuel', 'cb', 'mafia', '', '1');</script><script>update_player('1', '5', 'deisler', 'cmf', 'free_kick', '1', '');</script><script>update_player('1', '6', 'ferdinard', 'cb', 'strong', '1', '');</script><script>update_match('2', 'Match 2', '0');</script>

When I had the PHP code that produced these scripts in the bottom of the index.php, all the js functions where called correctly. 当我在index.php的底部有生成这些脚本的PHP代码时,正确调用了所有js函数。 Because of this question though, I had to move the code to another .php file. 但是由于这个问题 ,我不得不将代码移到另一个.php文件中。

Do it this way: 这样做:

In PHP... 在PHP中...

$arrayOfCalls = array();
$arrayOfCalls[]="update_match('1')";
$arrayOfCalls[]="update_match('2')";

$dummy = array();
$dummy['calls'] =$arrayOfCalls;
echo json_encode($dummy);

And in Javascript... 而用Javascript ...

$.post('php/main.php', {elements: 1}, function(return_json) {
    return_json = JSON.parse(return_json);
    return_json.calls.forEach(function(code){
    eval(code);
    })
});

You can append the result to your page : 您可以将结果附加到页面:

$.post('php/main.php', {elements: 1}, function(return_msg) {
    $('body').append(return_msg);
});

The code will be executed but I'm not sure if it's safe to do that. 该代码将被执行,但是我不确定这样做是否安全。

The date delivered by the AJAX call is just data - or text. AJAX调用传递的日期仅仅是数据或文本。 In order to interpret that as JavaScript, you must append it to the dom. 为了将其解释为JavaScript,必须将其附加到dom。

Two ways: first, by appending the data directly: 两种方法:首先,通过直接附加数据:

$.post('php/main.php', {elements: 1}, function(return_msg) {
    alert(return_msg);
    $('body').append(return_msg);
});

but there's also a shorthand method : 但是还有一种简化方法

$.getScript('php/main.php?elements=' + 1, function () {
   // script loaded

});

Please read the docs, there are some caveats! 请阅读文档,有一些警告!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM