执行脚本AJAX回报 [英] Execute scripts AJAX returns

查看:161
本文介绍了执行脚本AJAX回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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

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

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

下面是响应:

<$p$p><$c$c><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;

当我不得不产生这些脚本中的index.php的底部,所有正确的地方叫JS功能的PHP code。因为这个<一href="http://stackoverflow.com/questions/32554248/cant-get-global-variable/32554401#32554401">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);
    })
});

这篇关于执行脚本AJAX回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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