尝试使用put覆盖邮政方法的错误(不允许的方法)

发布于 2025-01-27 14:37:08 字数 1427 浏览 4 评论 0原文

当尝试用put覆盖我的帖子请求时,我正在遇到一个“不允许”的问题(用于更新我的博客中的信息)。我已经安装了KOA的方法。

HTML:

  <div class="create-message content">
<form action="/messages/edit/<%= message.id %>?_method=PUT" method="POST">
  <label for="title">Message title:</label>
  <input required value="<%= message.title %>" type="text" id="title" name="title" required>
  <label for="snippet">Message snippet:</label>
  <input required value="<%= message.snippet %>" type="text" id="snippet" name="snippet" required>
  <label for="body">Message body:</label>
  <input required value="<%= message.body %>" type="text" id="body" name="body" required>
  <button>Update</button>
</form>
My routs are the following:
  //edit message
  router.get('/messages/edit/:id', async (ctx, next) => {
    const id = ctx.params.id;
    const result = await MessageModel.findById(id)
    await  ctx.render('edit', {
      title: 'Messages',  
      message: result
     })
    });

上面的代码运行良好,但是在我单击“提交”按钮后,“方法不允许”问题而不是运行:

  //update edited message
  router.put('/messages/edit/:id', async (ctx, next) => {
      MessageModel.findByIdAndUpdate(ctx.params.id, ctx.request.body, {new:true}, (err:any, result:any) => {
      })
    return ctx.redirect('/');
});

请分享您对此问题的想法。 谢谢

I am experiencing a "Method Not Allowed" issue when trying to override my POST request with PUT (for updating information in my blog). I already installed method override for koa.

HTML:

  <div class="create-message content">
<form action="/messages/edit/<%= message.id %>?_method=PUT" method="POST">
  <label for="title">Message title:</label>
  <input required value="<%= message.title %>" type="text" id="title" name="title" required>
  <label for="snippet">Message snippet:</label>
  <input required value="<%= message.snippet %>" type="text" id="snippet" name="snippet" required>
  <label for="body">Message body:</label>
  <input required value="<%= message.body %>" type="text" id="body" name="body" required>
  <button>Update</button>
</form>

My routs are the following:

  //edit message
  router.get('/messages/edit/:id', async (ctx, next) => {
    const id = ctx.params.id;
    const result = await MessageModel.findById(id)
    await  ctx.render('edit', {
      title: 'Messages',  
      message: result
     })
    });

The code above runs well, but after I click on submit button, "Method Not Allowed" issue occurs instead of running this:

  //update edited message
  router.put('/messages/edit/:id', async (ctx, next) => {
      MessageModel.findByIdAndUpdate(ctx.params.id, ctx.request.body, {new:true}, (err:any, result:any) => {
      })
    return ctx.redirect('/');
});

Please share your thoughts on this issue.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情愿 2025-02-03 14:37:08

确保将以下内容添加到您的app.js(index.js)文件中。

app.use(methodOverride('_method'));

Make sure you add the following to your app.js (index.js) file.

app.use(methodOverride('_method'));
丢了幸福的猪 2025-02-03 14:37:08

不能设置放置的表单方法,它是获取或发布的。

要发送put请求,您可以使用ajax,例如(例如) fetch api

编辑:我不好,您使用了该解决方案中提到的“隧道”概念。

per 文档,也许可以尝试

<form method="POST" action="/resource" enctype="application/x-www-form-urlencoded"> 
<input type="hidden" name="_method" value="PUT">

You can't set the form method to PUT, it's either GET or POST.

to send a PUT request you can use AJAX, via (for instance) the Fetch API

EDIT: my bad, you used the "tunneling" concept mentioned in that solution.

per this documentation, maybe try with

<form method="POST" action="/resource" enctype="application/x-www-form-urlencoded"> 
<input type="hidden" name="_method" value="PUT">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文