我使用JSON字符串从PHP在JavaScript中传递变量:从PHP传递变量给JavaScript
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
,我使用下面的代码片段:
console.log(result) ;
var json = eval('('+ result +')') ;
和出现在控制台中的是以下错误:
1{"data":[{"id":"1","alert_type_id":"1","deviation":null,"threshold_low":"20","threshold_high":"80"}]}
SyntaxError: Expected token ')'
请问您能帮我解决这个问题吗? 非常感谢。
是不是像:'('+ result +')'我很抱歉,但我没有得到什么错 – user690182 2012-03-10 20:54:30
@ user690182。不,这不对。它只会在'()'侧面运行字符串“command”。你想用'json'做什么? – gdoron 2012-03-10 20:56:54
谢谢,我写了你刚刚在PHP中所说的:echo'('。'{“data”:'。json_encode($ settings)。'}'。')';现在一切正常:)谢谢 – user690182 2012-03-10 20:58:06