简体   繁体   English

您能否在重定向期间将数据发送回以响应应用程序

[英]Can you send data back to react app during a redirect

I apologize if this is a duplicate but I can't seem to find an exact answer. 抱歉,如果这是重复的,我似乎找不到确切的答案。 I'm either searching wrong or it's not possible. 我搜索错误,或者这不可能。 The current flow for my site is for a user to login, be authenticated and then redirected to another route. 我网站的当前流程是让用户登录,进行身份验证然后重定向到另一条路由。 At this route several components would need to load (right now I have it setup via ajax requests) but I'd like to already have this done. 在这条路线上,需要加载几个组件(现在我已经通过ajax请求对其进行了设置),但是我想已经完成了。

When I redirect I've already looked up all the user info I need and don't like the idea of making a seperate db request when it's already been done. 当我重定向时,我已经查找了我需要的所有用户信息,并且不喜欢在已经完成时发出单独的数据库请求的想法。 Is it possible to send data along with res.redirect() that can be read by react before components mount? 是否可以将数据与res.redirect()一起发送,以便在组件安装之前由react读取?

So, for example if the user presses login I make a post request to the server. 因此,例如,如果用户按下登录名,我向服务器发出发布请求。 The server then handles the login, creates a session, and in doing so has already looked them up in the database. 然后,服务器处理登录,创建会话,并且已经在数据库中查找了会话。

After they are redirected the landing page displays their name. 重定向后,登录页面将显示其名称。 What I am currently doing calling another ajax request just after the component on the landing page mounts. 我目前正在执行的操作是在登录页面上的组件挂载之后调用另一个ajax请求。 However what I'd like to do is send the name along with the redirect so I don't have to touch the db with a separate call. 但是,我想做的是将名称和重定向一起发送,因此我不必通过单独的调用来接触数据库。

UPDATE: please note i'm using react, not a view template 更新:请注意,我使用的是React,而不是视图模板

In your case, you might want to use a plugin that will persist the state even after redirect then "rehydrates" your store with the persisted state. 在您的情况下,您可能想要使用即使在重定向后仍可保持状态的插件,然后用持久状态“重新水化”商店。 You can check: 您可以检查:

https://github.com/rt2zz/redux-persist https://github.com/rt2zz/redux-persist

With the limited clarity I got from Your question is not clear. 由于清晰度有限,我从您的问题中不清楚。 Response.redirect is server side call, better call to an end point on login authentication call back which returns the data required for your react views to load after login. Response.redirect是服务器端调用,可以更好地调用登录身份验证回调上的端点,它返回登录后加载您的反应视图所需的数据。 Once you have data you can load your views. 一旦有了数据,就可以加载视图。

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

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