gpt4 book ai didi

javascript - 方法-重写 put 请求

转载 作者:行者123 更新时间:2023-12-03 02:26:16 26 4
gpt4 key购买 nike

在我的 app.js 中,我需要方法覆盖以及 app.use(methodOverride("_method"))

这个

显示了我的错误,我注意到该错误不包含像网址中那样的 ?_method=PUT。

这是链接到我的 app.js 的 ejs 文件

<% include ../partials/header %>
<div class = "container">
<div class="row">
<h1 style = "text-align: center;">Edit <%= campground.name %></h1>
<div style = "width: 30%; margin: 25px auto;">
<form method = "POST" action ="/campgrounds/<%= campground._id %>?_method=PUT">
<div class="form-group">
<input class = "form-control" type = "text" value="<%= campground.name %>" name = "campground[name]">
</div>
<div class="form-group">
<input class = "form-control" type = "text" value="<%= campground.image %>" name = "campground[image]">
</div>
<div class="form-group">
<input class = "form-control" type = "text" value="<%= campground.description %>" name = "campground[description]">
</div>
<div class="form-group">
<button class="btn btn-lg btn-default btn-primary btn-block">Submit!</button>
</div>
</form>
<a href="/campgrounds/<%= campground._id %>">Go back</a>
</div>

</div>
</div>


<% include ../partials/footer %>

这是我的编辑/更新路线

//Edit campground route
router.get("/:id/edit", function(req, res){
Campground.findById(req.params.id, function(err, foundCampground){
if(err){
res.redirect("/campgrounds");
} else{
res.render("campgrounds/edit", {campground: foundCampground});
}
});

});
//Update campground route
router.post("/:id", function(req, res){
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, updatedCampground){
if(err){
res.redirect("/campgrounds");
} else{
res.redirect("/campgrounds/" + req.params.id);
}
});
});

最佳答案

您已经重写了方法类型,因此它被视为 PUT 请求。

您有 router.post("/:id", 但没有 router.put("/:id",

由于没有与 URL 和方法匹配的路由,因此您会收到错误。

关于javascript - 方法-重写 put 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48935924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com