0

我正在使用 Node、Express、handlebars 和 MongoDB 构建一个购物车项目。目前,当我尝试使用 Jquery 提交表单时,我的“/checkout”文件收到 404 错误。最初,我使用return false来防止表单在收到数据之前提交。为了解决这个问题,我也尝试使用event.preventDefault。在另一个论坛上,有人建议我的crsf保护没有正确创建令牌。我使用该路线并添加到我的视图中,最后添加到hbs表单中。这些尝试似乎都没有解决我的问题。

这是我的要点的链接。https://gist.github.com/Satellite9/5e4ce3de5c19cee2f355d872b6d7d3c8

这是我的浏览器发送给我的错误。

*错误:在 C:\Users\Leimamo\PhpstormProjects\untitled2\app.js:60:13 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\ router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\ untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index .js:635:15 在下一个(C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:260:14) 在 Function.handle (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:174:3) 在路由器 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\ express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5)在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\ index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\Users\Leimamo\PhpstormProjects \untitled2\node_modules\express\lib\router\index.js:275:10) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\app.js:51:5 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2 \node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C :\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12)在下一个(C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index. js:275:10) 在 serveStatic (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\serve-static\index.js:75:16) 在 Layer.handle [as handle_request] (C:\Users\Leimamo\ PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284 :7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\ express\lib\router\index.js:275:10) 在 SessionStrategy.strategy.pass (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\passport\lib\middleware\authenticate.js:325:9) *12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 SessionStrategy.strategy.pass (C:\Users\Leimamo\PhpstormProjects\untitled2\ node_modules\passport\lib\middleware\authenticate.js:325:9) *12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 SessionStrategy.strategy.pass (C:\Users\Leimamo\PhpstormProjects\untitled2\ node_modules\passport\lib\middleware\authenticate.js:325:9) *

这是npm给我的错误 Post /checkout 404 206.855 ms -5424

感谢您提供的任何见解。

基隆

4

2 回答 2

1

您在 index.js 上使用了错误的动词。更改获取-> 发布。

router.get('/checkout', function(req, res, next) {
......
});

router.post('/checkout', function(req, res, next) {
....
});
于 2017-06-19T06:37:06.473 回答
0

用我的演示编辑,伙计。

router.get('/checkout', function(req, res, next) {
 //do something
    });

router.post('/checkout', function(req, res, next) {
// do something: get params with req.body
    });

我使用路线并添加到我的视图中,最后添加了 hbs 表单

hbs 表单:我在提交表单时使用带有 post 的路由

于 2017-06-19T07:51:09.867 回答