首页 > 解决方案 > 收到获取请求后如何显示页面?

问题描述

我试图在收到获取请求后显示一个页面。例如,一些用户向我的服务器('/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'。我想在服务器端更改页面。

标签: javascriptnode.jsexpress

解决方案


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).


推荐阅读