简体   繁体   English

在node.js中返回response.sendRedirect返回JSON并且不呈现页面

[英]return response.sendRedirect in node.js is returning JSON and not rendering page

I am using response.redirect in my node.js application using express 4 and the redirect code is returning the 302 redirection response in JSON but not rendering the page 我在我的node.js应用程序中使用express 4使用response.redirect,并且重定向代码以JSON返回302重定向响应,但未呈现页面

Here is my simple redirection code 这是我简单的重定向代码

app.post('/signup', function (req, res) {
    console.log("Got signup request");
    return res.redirect('/login');
});

I tried res.render as well still no luck. 我也尝试过res.render仍然没有运气。

The funny part is, I do get a request on my login module, but how do I render the page? 有趣的是,我确实在登录模块上收到了一个请求,但是如何呈现页面? The only option I can think of right now is to read the JSON response in the UI and then redirect it to Login page. 我现在唯一想到的选择是在UI中读取JSON响应,然后将其重定向到“登录”页面。 But this will: 但这将:

  1. Hit two requests to the target page (right now it is login, but imagine if it is some other major page) 命中两个请求到目标页面(现在是登录名,但想想它是否是其他主要页面)
  2. Expose the logic of redirection, which is a bad idea 公开重定向的逻辑,这是一个坏主意
  3. Cannot transfer the data from one page to the other (I do not want to use localStorage or sessionStorage here) 无法将数据从一页转移到另一页(我不想在这里使用localStorage或sessionStorage)

Your kind help will be much appreciated Thanks in advance. 非常感谢您的帮助。

EDIT: 1 Login route 编辑:1登录路线

app.get('/login', function (req, res) {
    console.log("Got a login request");
    res.sendFile(GLOBAL.basePath+'/resources/login.html');
});

Alright! 好的! Express cannot do a redirection from an ajax call. Express无法从ajax调用进行重定向。 The redirection logic then has be taken care from the ajax success response. 然后,已经从ajax成功响应中考虑了重定向逻辑。

res.redirect from POST 来自POST的res.redirect

Express js - can't redirect Express JS-无法重定向

There are many other links to help on this issue 还有许多其他链接可以帮助您解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM