Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Express中间件中丢失ajax数据_Javascript_Node.js_Express - Fatal编程技术网

Javascript 在Express中间件中丢失ajax数据

Javascript 在Express中间件中丢失ajax数据,javascript,node.js,express,Javascript,Node.js,Express,我有一个有授权用户登录的Node/Express应用程序。然后,我让该用户使用数据对受保护的路由进行ajax调用。在继续路由之前,我必须确保用户已通过身份验证。数据在ajax调用和到达路由之间丢失。有没有一种方法可以防止数据在这个中间件中丢失 前端.js文件 $.ajax({ url: "/voted", method: "POST", data: { item1: "some data", item2: "other data" }, success: () => { c

我有一个有授权用户登录的Node/Express应用程序。然后,我让该用户使用数据对受保护的路由进行ajax调用。在继续路由之前,我必须确保用户已通过身份验证。数据在ajax调用和到达路由之间丢失。有没有一种方法可以防止数据在这个中间件中丢失

前端.js文件

$.ajax({
  url: "/voted",
  method: "POST",
  data: { item1: "some data", item2: "other data" },
  success: () => { console.log("success") },
  failure: () => { console.log("failure") }
});
路由中间件

// data being lost here
const protectedMiddleware = (req, res, next) => {

  if (req.isAuthenticated()) {
    next();
  }
  else {
    res.redirect("/login");
  }
}
my routes.js文件中的路由

app.post("/voted", protectedMiddleware, (req, res) => {

  let item1 = req.query.item1;
  let item2 = req.query.item2;
  console.log(item1); // undefined
  console.log(item2); // undefined

});

对于item1和item2,我没有定义,而它们应该是“一些数据”和“其他数据”。

req.query
是从url中的查询参数设置的。因此,如果请求url看起来像
/voted?item1=somedata
,则可以访问
req.query.item1

您要查找的是
req.body
,因为您正在请求的
body
中传递数据


您可以访问
item1
item2
作为
req.body.item1
req.body.item2

req.query
是从url中的查询参数设置的。因此,如果请求url看起来像
/voted?item1=somedata
,则可以访问
req.query.item1

您要查找的是
req.body
,因为您正在请求的
body
中传递数据

您可以访问
item1
item2
作为
req.body.item1
req.body.item2