Javascript 方法重写put请求
在我的app.js中,我需要方法覆盖,也需要Javascript 方法重写put请求,javascript,node.js,express,web-development-server,Javascript,Node.js,Express,Web Development Server,在我的app.js中,我需要方法覆盖,也需要app.use(methodOverride(“\u method”) 这个 显示我的错误,我注意到错误不包括?\u method=PUT,就像url中的内容一样 这是链接到我的app.js的ejs文件 <% include ../partials/header %> <div class = "container"> <div class="row"> <h1 style = "te
app.use(methodOverride(“\u method”)
这个
显示我的错误,我注意到错误不包括?\u method=PUT,就像url中的内容一样 这是链接到我的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 %>
您已重写该方法类型,因此将其视为PUT请求 你有
router.post(“/:id”,
,但没有router.put(“/:id”,
)
由于没有与URL和方法相匹配的路由,因此会出现错误。“我注意到错误不包括?\u method=PUT like in the URL”-这无关紧要。它已被清楚识别,因为错误消息说它正在尝试放置数据。
//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);
}
});
});