将变量从PHP传递到JavaScript [英] pass a variable from PHP to JavaScript

查看:65
本文介绍了将变量从PHP传递到JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON字符串从PHP传递JavaScript中的变量:

I am using a JSON string to pass a variable in JavaScript from PHP :

while( $row = mysql_fetch_array($result) )
        {
                $tmp = array('id'=>$row['id'], 'alert_type_id'=>$row['alert_type_id'], 'deviation'=>$row['deviation'], 'threshold_low'=>$row['threshold_low'], 'threshold_high'=>$row['threshold_high']) ;
                $settings[] = $tmp ;
        }

        echo '{"data":'.json_encode($settings).'}' ;

在Javascript中,我正在使用以下代码段:

in Javascript, i am using the following snippet :

console.log( result ) ;                
var json = eval('('+ result +')') ;

,控制台中出现以下错误:

and what appears in the Console is the following error :

1{"data":[{"id":"1","alert_type_id":"1","deviation":null,"threshold_low":"20","threshold_high":"80"}]}

SyntaxError: Expected token ')'

能否请您帮助我解决这个问题?非常感谢.

Could you please help me overcome this issue please ? Many Thanks.

推荐答案

那行代码无效.

在控制台中写入:

'(1{"data":[{"id":"1","alert_type_id":"1","deviation":null,"threshold_low":"20","threshold_high":"80"}]})'

这就是您要传递给 eval 函数的内容.我应该给你同样的错误.

As this is what you're passing to the eval function. I should give you the same error.

如果要将该字符串保存在变量中,则需要对其进行一些调整:

If you want to save that string in a variable you need to adjust it a bit:

eval('var x =' + result);

无论如何,看来您做错了什么,请再次检查为什么需要使用邪恶的" eval .

Anyway It looks like you're doing something wrong, check again why do you need ti use the "evil" eval.

这篇关于将变量从PHP传递到JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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