javascript - 收到获取请求后如何显示页面?
问题描述
我试图在收到获取请求后显示一个页面。例如,一些用户向我的服务器('/home')发送了一个获取请求,然后我想显示主页。
我试过 res.render 但它不会改变页面。它只发回网址。
fetch('lom').then((res)=>{
console.log(res);
}); //GET Request
app.get('/lom',(req,res)=>{
res.render('lom');
}); // Respond
我希望在请求后看到 /home 页面,但当前页面没有改变。我不想使用 window.location.href = '/lom'。我想在服务器端更改页面。
解决方案
You're using fetch
.
The entire point of using fetch
is that the browser doesn't navigate to a new page, and the response is processed using JavaScript instead.
Use a regular link. Submit a form. Use window.location.href = '/lom'
(I know you said you didn't want to, but not wanting to do the right thing is a terrible reason not to do it). But do something which causes the browser to navigate. fetch
is the wrong tool for the job.
I want to change page in server-side.
There is no way to trigger navigation in the browser directly from the server-side. There needs to be something on the client designed to navigate (this might be triggered by client-side code based on data in a response from the server, but it still needs to be client-side).
推荐阅读
- r - R图线性拟合子集
- python-3.x - 迭代添加的最有效数据类型?
- javascript - 如何比较两个数字一是相同输入的输入值和属性值?
- mysql - MySql 查询速度慢 1000 倍,LIMIT 稍高(不是偏移量)
- android - BLE:如何在扫描结果之间准确找到我的设备?
- python - python中类实例化期间实例与对象之间的区别
- javascript - 通过 Symbol.iterator 的迭代不会发生
- powershell - 如何使用 powershell 5 创建具有 2 个密钥用法的证书
- database - 如何在不覆盖实时数据的情况下更新来自测试数据库的实时数据库中的列内的表
- php - Angular 5 Ionic 3 Laravel 项目结构