繁体   English   中英

我正在使用 cakephp 2.x,我需要将数据从特定控制器传递到 javascript 文件

[英]I'm using cakephp 2.x, I need to pass data from a specific controller to a javascript file

$results = $this->Annonce->find('all', 
               array(
                     'conditions' => array('Annonce.name' => $name),
                     'recursive' => -1
      )
);

我需要在我的 javascript 文件( /app/webroot/js/filenames.js )中使用 $results 的值

由于您不能在.js文件中使用 php 代码,您可以做的是将$results数组放在一个 javascript 变量中并在.js文件中使用它。

例如

<script type="text/javascript">
    var results= <?php echo json_encode($results); ?>;
</script>

现在你可以在 javascript 文件中使用这个变量"results"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM