简体   繁体   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
      )
);

I need to use the value of $results in my javascript file ( /app/webroot/js/filenames.js )我需要在我的 javascript 文件( /app/webroot/js/filenames.js )中使用 $results 的值

As you cannot use a php code in .js file, what you can do is put the $results array in a javascript variable and use that in .js file.由于您不能在.js文件中使用 php 代码,您可以做的是将$results数组放在一个 javascript 变量中并在.js文件中使用它。

eg例如

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

Now you can use this variable "results" in javascript file现在你可以在 javascript 文件中使用这个变量"results"

暂无
暂无

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

相关问题 cakephp 2.x在javascript中执行控制器 - cakephp 2.x execute controller in javascript 我正在尝试使用 ajax 将数组从 controller 传递到 javascript 但它不会以数组形式返回,而是以字符串形式返回 - I'm trying to pass an array from controller to javascript by using ajax but it doesnt return as array it returns as a String 从html + javascript cakePHP 2.x生成PDF - generating PDFs from html+javascript cakePHP 2.x 如何将数据存储区值传递给javascript? 我需要从gae数据存储区获取数据并需要传递javascript - How to pass the datastore value to javascript? i need to get data from the gae datastore and need to pass javascript 我需要传递期望的参数。 标记文件,使用javascript - I need to pass a parameter expected by a . tag file, using javascript 我无法将推送数据到Firebase数据库以及其他javascript文件时获得的UID传递给我 - I'm not able to pass the UID that I get from pushing data to my firebase db, to other javascript files 我需要从其他JavaScript文件访问数据,并将其更改为字符串 - I need to access data from a different javascript file, and change it into a string 需要Javascript或jquery代码才能将数据传递到控制器 - Need Javascript or jquery code to pass data to controller 如何使用Ajax将表单值传递给控制器​​(控制器部分需要哪种类型来接受发送的数据) - How to pass form value to controller using Ajax (Which type do i need in the controller part to accept sent data) 将数据从Controller传递给JavaScript - Pass Data from Controller to JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM