简体   繁体   中英

Why is my form still requesting with POST even though has hidden input DELETE?

I'm building my first Node.js app and I'm trying to make a request to delete a model. Below is the show.ejs where I'm attempting to place a button for sending the delete request & the corresponding controller method to delete the model. I'm getting an error that the request is still being sent as a post request and I'm not sure why. Any ideas why?

show.ejs

<html>
  <h1><%= movie.title %></h1>
  <img id="poster" src="<%= movie.poster %>">

  <p>Release Date: <%= movie.releaseDate %></p>
  <p>Critics Score: <%= movie.criticsScore %></p>
  <p>Audience Score: <%= movie.audienceScore %></p>

  <p><a href="/movies">Back</a></p>

  <form action="/movies/<%= movie._id %>" method="post">
    <input type="hidden" name="_method" value="delete">
    <input data-confirm='Are you sure?' value="Delete Movie" type="submit">
  </form>
</html>

movies.js

app.delete('/movies/:id', function (req, res) {
  Movie.findById(req.params.id, function (err, movie) {
    Movie.remove({ title: movie.title })

    res.redirect('/movies');
  });
});

I added app.use(express.methodOverride()); to my config.js and the issue is resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM