CakePHP4:Link 到 Json 脚本中的文件

CakePHP4: Link to Json File in Script

我有一个 CakePHP4 应用程序,我在其中的 <script> 标记中创建了一个 D3JS WorldMap。我需要 link 到 JSON 这一行的文件 d3.json("https://api.jsonbin.io/b/61f8ec14518e5f3b2ab3e218", function(error, topology) { 但这次它保存在 webroot folder.

我已经尝试获取路径 console.log('<?php echo WWW_ROOT; ?>'); 但没有成功。如何通过 JavaScript 为我的文件设置路径?

How can I set the Path to my file over JavaScript?

path: "<?= $this->Url->build('/my.json', ['fullBase' => true]); ?> "

// Output
// path: "http://somedomain.com/my.json"

或:

<div data-json="<?= $this->Url->build('/my.json', ['fullBase' => true]); ?>" ></div>

然后在你的js中读取数据属性

阅读更多: https://book.cakephp.org/4/en/views/helpers/url.html