dongteng2534 2013-03-21 09:35
浏览 102

php javascript json解析转义字符

I currently have a webpage that need to use javascript to parse variables from php.
I do things like this:

data.notices = JSON.parse('<?php echo json_encode($notices) ?>');

However, when there is single or double quotes in the $notices variable, javascript console return errors.

How can I get the variables correctly?

  • 写回答

4条回答 默认 最新

  • douna3367 2013-03-21 09:41
    关注

    This code doesn`t return error

    <?
    $notices = array('sad'=>'asd as" asd', 'asd"sdf '=>'asdasd" \' asd ads');
    ?>
    <script>
    data = new Object();
    data.notices = JSON.parse('<?php echo addslashes(json_encode($notices)) ?>');
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试