简体   繁体   English

用重定向传递对象数组的正确方法是什么?

[英]What is the right way to pass array of objects with redirect?

I'm using node.js and I need to pass an array of objects with res.redirect() , I tried with querystring but it's not a good idea because the array with too big and I could get Error 414 Request URI too long . 我正在使用node.js,我需要使用res.redirect()传递对象数组,我尝试使用querystring,但这不是一个好主意,因为该数组太大,并且我可能会收到Error 414 Request URI too long using connect-flash isn't a good way either, it's more useful for passing messages. 使用connect-flash也不是一个好方法,它对于传递消息更有用。 And I don't want to use req.app.locals for that. 而且我不想为此使用req.app.locals

Hope you can help me with any idea. 希望您能提供任何帮助。

Assuming the client here is a browser that you want to just automatically follow the redirect and then inherit some new state when the server generates the page for that newly redirected URL, then here are some options: 假设客户端是一个浏览器,您只想自动遵循重定向,然后在服务器为该新重定向的URL生成页面时继承一些新状态,那么这里有一些选项:

  1. If you already have a session established for the user, then you can store the data in the session, then include a single query parameter that tells the route handler for the page you're redirecting to to look in the session to get the relevant data. 如果您已经为用户建立了会话,则可以将数据存储在会话中,然后包含一个查询参数,该参数告诉要重定向到的页面的路由处理程序以在会话中查找以获取相关数据。

  2. You could also create a temporary server-side cache of data. 您还可以创建服务器的临时数据缓存。 Generate a random key (likely a timestamp plus a random number). 生成一个随机密钥(可能是时间戳加一个随机数)。 Store the data in a server-side Map using that key. 使用该密钥将数据存储在服务器端Map中。 Then put the key into a query string on the redirect. 然后将密钥放入重定向中的查询字符串。 Then, in the route handler for the new, redirected page, it will see the query string parameter and it can grab that key out of the query string and access the data from the server-side Map serving as a temporary cache (and probably remove it from the cache too). 然后,在新的重定向页面的路由处理程序中,它将看到查询字符串参数,并且可以从查询字符串中获取该键,并从用作临时缓存的服务器端Map访问数据(并可能删除也可以从缓存中获取)。 This scheme works in a session-less environment. 此方案在无会话的环境中工作。

    You then need some scheme for cleaning up unused data from the cache so it doesn't accumulate. 然后,您需要一些方案来从缓存中清除未使用的数据,以使其不会累积。 Probably what makes sense it to timestamp the data and then have a setInterval() timer that just removes things from the Map if their timestamp is older than xx minutes. 可能有必要对数据加时间戳,然后使用setInterval()计时器,如果它们的时间戳早于xx分钟,则该计时器仅将其从Map删除。

If the request is an Ajax call rather than a regular browser page request, then you don't need to use a redirect at all. 如果该请求是Ajax调用而不是常规浏览器页面请求,那么您根本不需要使用重定向。 You can just return the content that they would have gotten if they then followed your redirect. 您可以仅返回他们在遵循重定向后获得的内容。 Then, you don't have to invent a temporary place to store the data. 然后,您不必发明一个临时的位置来存储数据。 You can just use the data to generate the desired page and return it. 您可以只使用数据生成所需的页面并返回它。

Store the data as session data and serve it back up to them when they land on the landing page. 将数据存储为会话数据,并在它们到达目标页面时将其提供给他们。

Another way would be to have the client request the data asynchronously once the redirect has been performed. 另一种方法是让客户端在重定向完成后异步请求数据。

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

相关问题 将2个javascript对象连接在一起的正确方法是什么? - What is the right way to wire together 2 javascript objects? 将参数传递给回调函数的正确方法是什么? - What is the right way to pass parameters into callback functions? 如何重命名数组中对象的属性。 解决任务的正确方法是什么? - How to rename properties in objects that are in an array. What is the right way to solve the task? Laravel API,在一个 POST 请求中传递多个对象的正确方法 - Laravel API, the right way to pass multiple objects in one POST request 将这个数据库结果传递给javascript函数的正确方法是什么? - What's the right way to pass this database result into a javascript function? 将数据从PHP应用程序传递到JS的正确方法是什么? - What is the right way to pass data from PHP application to JS? 将一些初始数据传递到新窗口的正确方法是什么? - What's the right way to pass some initial data to a new window? 更新对象数组的另一种方法是什么? - What is an alternative way to update an array of objects? 遍历这个对象数组的最佳方法是什么? - What is the best way to loop through this array of objects? 减少这样一个对象数组的最佳方法是什么? - What is the best way to reduce such an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM