【问题标题】:Can't get ejs file无法获取ejs文件
【发布时间】:2018-01-24 08:46:07
【问题描述】:

我需要在另一个 ejs 文件中调用一个 ejs 文件并且完全在函数中,但我总是“无法获取文件” 如果我在函数之外做一个包含文件。它可以工作。 但是我需要在函数中调用这个ejs文件。

<input type = "submit" value = "heure"  id="sub" style="width:120px" onclick="changer()"/>
<input type = "submit" value = "journée" id="sub1" style="width:120px" onclick="changersub1()"/>

 <script type="text/javascript">
    function changersub1()
    {
         document.getElementById("sub1").style.backgroundColor="MistyRose";

         document.getElementById("sub").style.backgroundColor="white";

         window.location = "./index1.ejs";
    } 
 </script>

请问你有什么想法吗?谢谢

【问题讨论】:

  • 你不能直接从客户端访问ejs文件。你需要创建一个路由来渲染index1.ejs然后把它添加到你的客户端window.location = "/route-url";

标签: javascript node.js express ejs


【解决方案1】:

下面一行

window.location = "./index1.ejs";

你试图调用类似 localhost:5000/index1.ejs 的东西,这是错误的。 Index1.ejs 是一个模板文件,在ExpressJS

中需要在服务端渲染如下
router.get('/index1', function(request, response) {
   response.render('index1.ejs');
}); 

将以上行添加到您的路线并将功能更改为跟随

window.location = "/index1";

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 2022-01-07
    • 2019-01-21
    • 1970-01-01
    • 2022-11-14
    • 2020-10-06
    • 2016-10-10
    • 2020-07-20
    • 2017-09-11
    相关资源
    最近更新 更多