NodeJS Express发布请求始终返回404页面,但它确实返回正确的页面

编程入门 行业动态 更新时间:2024-10-10 15:24:23

NodeJS Express发布请求始终返回404<a href=https://www.elefans.com/category/jswz/34/1771336.html style=页面,但它确实返回正确的页面"/>

NodeJS Express发布请求始终返回404页面,但它确实返回正确的页面

对NodeJS来说还很陌生,我正在编写一个分页脚本来显示数据库数据。要使用表单按钮导航到下一个结果,因为这是我与Node进行按钮交互的唯一方法。我知道这不是最佳做法,但现在足以满足我的需求:)但是,当用户单击“下一个/上一个”按钮时,确实会调用发帖请求,但总是返回404页,而如果我复制该确切的页面,返回页面并将其加载到我自己中,页面加载正常。

发布功能:

  if(req.body.next){
    console.log(req.body.next)
    start += 100;
    console.log(start);
    get_products(start);
  }
  if(start >= 0 && req.body.prev){
    console.log(req.body.prev)
    start -= 100;
    console.log(start);
    get_products(start);
  }
});

哈巴狗形式:

form(method = 'POST', action="/products")
     button(type='submit', name='prev', value='prev') previous
     button(type='submit', name='next', value='next') next

帖子的输出:

next
100
POST /products - - ms - -
prev
0
POST /products - - ms - -
next
100
POST /products - - ms - -
next
200
POST /products - - ms - -
next
300
POST /products - - ms - -
next
400
POST /products - - ms - -
prev
300
回答如下:

@@ montrealist的评论回答了我的问题。我必须在帖子调用中添加res.render,以使该函数刷新页面。

更新后的帖子:

router.post('/', function(req, res, next) {
  if(req.body.next){
    start += 100;
    get_products(start);
    res.render('products' , {
      title:"All products from index: " + (start - 99).toString(),
      products:products
    })
  }
  if(start >= 0 && req.body.prev){
    start -= 100;
    get_products(start);
    res.render('products' , {
      title:"All products from index: " + (start + 101).toString(),
      products:products
    })
  }
});

更多推荐

NodeJS Express发布请求始终返回404页面,但它确实返回正确的页面

本文发布于:2024-05-07 12:23:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   但它   确实   正确   NodeJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!