执行脚本AJAX回报脚本、AJAX

由网友(贱癌晚期以弃疗i)分享简介:提示:从PHP,你可以简单的发送一个JSON对象/数组,JS,并执行像每一个条目update_match('1');使用eval()函数,请停止发送JS code表示方式。 - 卢西恩Depold 在index.php,我有这个code,它执行时,该文件已准备就绪:$岗位(的PHP / main.php',{要素:1}...

提示:从PHP,你可以简单的发送一个JSON对象/数组,JS,并执行像每一个条目update_match('1');使用eval()函数,请停止发送JS code表示方式。 - 卢西恩Depold

在index.php,我有这个code,它执行时,该文件已准备就绪:

  $岗位(的PHP / main.php',{要素:1},功能(return_msg){
    警报(return_msg);
});
 

我得到的响应是一堆剧本,正如所料,与预期值。然而,它们不执行!如何让他们执行?

下面是响应:

<script>jsfunction(37069);</script><script>updateTextbox('');</script><script>update_match('1', '19 0.30星期五15/5','1');&其中; /脚本&GT;&其中;脚本&GT; update_player('1','1','雷科巴','CMF','TEO','0','0 ');&其中; /脚本&GT;&其中;脚本&GT; update_player('1','2','内斯塔','CB','tsoulou','0','0');和其中; /脚本&GT;&其中;脚本&GT; update_player('1','3','劳尔','比照','前锋','0','0');和其中; /脚本&GT;&其中;脚本&GT; update_player('1','4 ','塞缪尔','CB','黑手党','','1');&其中; /脚本&GT;&其中;脚本&GT; update_player('1','5','代斯勒','CMF',' free_kick','1','');&其中; /脚本&GT;&其中;脚本&GT; update_player('1','6','ferdinard','CB','强','1',''); &所述; /脚本&GT;&其中;脚本&GT; update_match('2','第2','0');和其中; /脚本&GT; 用ajax实现在单击事件下加载一个DIV层的脚本

当我不得不产生这些脚本中的index.php的底部,所有正确的地方叫JS功能的PHP code。因为这个question不过,我必须将code到另一个PHP文件。

解决方案

做这种方式:

在PHP ...

  $ arrayOfCalls =阵列();
$ arrayOfCalls [] =update_match('1');
$ arrayOfCalls [] =update_match('2');

$哑=阵列();
$假人['来电'] = $ arrayOfCalls;
回声json_en code($哑);
 

和Javascript中...

  $岗位(的PHP / main.php',{要素:1},功能(return_json){
    return_json = JSON.parse(return_json);
    return_json.calls.forEach(函数(code){
    的eval(code);
    })
});
 

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.

In the index.php, I have this code, which executes when the document is ready:

$.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. Because of this question though, I had to move the code to another .php file.

解决方案

Do it this way:

In PHP...

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

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

And in Javascript...

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

阅读全文

相关推荐

最新文章