Node.js how to pass Ajax success message to app.js, because i need to know user Log-in status

Node.js如何将Ajax成功消息传递给app.js,因为我需要知道用户登录状态

ex:If user have not logged in, redirect to log-in.html, so I need to know success message.

例如:如果用户尚未登录,请重定向到log-in.html,因此我需要知道成功消息。

index.html

...
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript>

            $.ajax({
                url: URLs,
                data: $('#sentToBack').serialize(),
                type:"POST",
                dataType:'text',

                success: function(msg){
                   if(msg == "True")
                   {
                     location.href = '/index.html';
                   }

                },

                 error:function(msg){ 
                    console.log(msg);
                 }
            });

</script>

app.js

var express = require('express');
var app = express();

app.get('/', function(req, res){
  // I want to send to here !!
});

app.listen(3000);

2 个解决方案

#1


There are many different ways to do this.

有很多不同的方法可以做到这一点。

This here

app.get('/', function(req, res){
  // I want to send to here !!
});

is defining the function that will handle get requests to your root route.

正在定义将处理对根路由的请求的函数。

If you are sending AJAX requests, you need to think about what METHOD (GET/POST/PUT/DELETE) you are using, and what CONTENT you are sending. You may need something to parse the body if you are, for example, using JSON.

如果您要发送AJAX请求,则需要考虑您正在使用的METHOD(GET / POST / PUT / DELETE)以及您要发送的内容。例如,如果您使用JSON,则可能需要一些东西来解析主体。

If you want to maintain session information and logins etc, I would suggest looking into some middleware like PassportJS.

如果你想维护会话信息和登录等,我建议你研究一些像PassportJS这样的中间件。

You can configure Passport to automatically redirect to the login page whenever a request is received from an unauthenticated user.

您可以将Passport配置为在收到未经身份验证的用户的请求时自动重定向到登录页面。

更多相关文章

  1. 如何在用户选择操作后获取当前日期和时间
  2. 使用Selectize和Ajax时,在Bootstrap模式中显示Rails错误消息
  3. 当AJAX响应来自PHP文件时,如何显示以消息为中心的加载器图像&防止
  4. Symfony 2在用户站点上动态添加字段以形成
  5. Yii - 加载ajax表单元素的用户端验证
  6. EasyUI动态展示用户信息
  7. Jquery - 表单验证,为错误消息添加css样式
  8. HTML5 windows和iframe之间传递消息
  9. j2ee的web项目,有最终的html代码(即f12看到的最终给用户浏览器展示

随机推荐

  1. PHP之十六个魔术方法详解(总结)
  2. 在PHP中处理JSON数组以及对象
  3. php结合layui前端实现多图上传
  4. docker-compose部署php项目
  5. PHP编码风格规范详细介绍
  6. PHP中 __toString()方法详解
  7. 安装docker和docker-compose
  8. PHP 实现守护进程
  9. PHP中 __wakeup()方法详解
  10. PHP中__debugInfo()魔术方法详解