【问题标题】:how to retrieve the nodejs session values in a html page using angularjs?如何使用 angularjs 在 html 页面中检索 nodejs 会话值?
【发布时间】:2018-04-03 18:30:12
【问题描述】:

我正在使用 express-session 在 nodejs 中创建会话并使用 http.post 方法将数据发送到 nodejs。如何使用angularjs检索html中的会话变量?你能帮帮我吗?

这是我的代码

在第一个 html 页面中的 angularjs 控制器内部,

   $http.post('/form', data)
       .then(
           function(response){
               if (response.data) {
                   $window.location.href = 'other.html'; 
                }
           }, 
           function(response){
               console.log("inside failure");
           }
        );

在nodejs中,

         app.post('/form', function (req, res) {
           console.log("Incoming data" , userData);
           req.session.mob = userData.mob;
           req.session.provider = userData.provider;
           res.send(req.session.mob);
         }
             

我必须在 nodejs 会话中存储的“other.html”中显示 mob 值。你能建议我如何通过 angularjs 来做吗?

【问题讨论】:

    标签: angularjs node.js express-session


    【解决方案1】:

    不确定我是否正确理解了您的问题 - 我会尽力提供帮助。 首先,使用路由或状态并将您从服务器接收到的数据(在您的示例中为 response.data)作为状态参数或 url 参数传递到新页面将是 angularJs 的最佳实践,并避免重定向。 如果您确实需要重定向到“other.html”,只需将来自服务器的数据作为 url 中的查询参数传递(如果它是一个属性)或将其保存在浏览器存储中(本地存储,如果正在打开新页面在新选项卡中)并在加载 other.html 时读取数据。

    确保将 url 传递给 $window.location.href。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多