gpt4 book ai didi

javascript - 如何从ejs页面重定向到另一个页面

转载 作者:行者123 更新时间:2023-11-30 19:28:11 26 4
gpt4 key购买 nike

我的代码是:

<%
var result = [];
if(result.length>0)
{
//do nothing
}
else
{
//redirect page to "/product-not-found"
//window.location.replace("http://stackoverflow.com"); is not working
}
%>

注意:不传递 res,req 来自 node.js Controller

当我添加这段代码时:

window.location.replace("http://stackoverflow.com");

它不适合我。显示错误:

window is not defined
at eval (eval at compile (\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:215:37)
at returnedFn (\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:482:10)
at View.render (\node_modules\express\lib\view.js:135:8)
at tryRender (\node_modules\express\lib\application.js:640:10)
at Function.render (\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (\node_modules\express\lib\response.js:1012:7)
at \controllers\productController.js:1274:8
at \node_modules\mongoose\lib\model.js:4733:16
at \node_modules\mongoose\lib\utils.js:263:16
at \node_modules\mongoose\lib\aggregate.js:973:13
at \node_modules\kareem\index.js:135:16
at processTicksAndRejections (internal/process/task_queues.js:82:9)

最佳答案

这里有一个你可以尝试的技巧

1-使用以下代码创建一个名为 redirect.ejs 的文件

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="refresh" content="1;url=<%= url %>">
<script type="text/javascript">
window.location.href = "google.com"
</script>
</head>
<body>
If you are not redirected automatically, <a href="<%= url %>">click here</a>.
</body>
</html>

2-像这样在您的代码中将此文件称为部分文件

<%
var result = [];
if(result.length>0)
{
//do nothing
}
else
{
%>
<%- include('redirect.ejs')%>// redirect to
<%
}%>

让我知道它是否适合你

关于javascript - 如何从ejs页面重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56698705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com