如何在Express和Node中从html表单发送放置请求 [英] how to send a put request from html form in express and node

查看:104
本文介绍了如何在Express和Node中从html表单发送放置请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于编辑对象的表单,我想使用带有node.js的express 3.x正确处理它

I have a form for editing an object and I want to handle it properly using express 3.x with node.js

编辑项目路径:/item/edit显示用于编辑对象的表单.

edit item route: /item/edit shows a form for editing the object.

我认为我有三个选择:

1)放置一个值为"edit"的隐藏字段,以便我可以正确处理它 这是更多的工作,因为我必须在app.post('/item', routes.item.post);中处理它,该文件还将处理新创建的内容和更新.

1) place a hidden field with a value of "edit" so I can handle it in express properly This is a little more work because I have to handle it in app.post('/item', routes.item.post); which also will handle new creations as well as updates.

2)仅使用jQuery.ajax()put调用提交编辑表单. 这使我可以使用app.put('/item', routes.item.put);

2) only submit the edit form with jQuery.ajax() put call. This allows me to use app.put('/item', routes.item.put);

3)将发帖请求发送到/item/edit而不是/item来处理更新/编辑 帖子将仅用于/item/edit上的更新:app.post('/item/edit', routes.item.edit.post);

3) send a post request to /item/edit instead of /item to handle the update/edit post will only be used for updating at /item/edit: app.post('/item/edit', routes.item.edit.post);

当查看app.js中的代码并遵循CRUD的标准约定时,解决方案#2是唯一直观且显而易见的解决方案.但是,如果由于某种原因未启用javascript,则他们将无法编辑其对象.

Solution #2 is the only one that is intuitive and obvious when looking at the code in app.js and follows standard convention for CRUD. But if javascript is not enabled for some reason, they cannot edit their object.

推荐答案

http://www.senchalabs.org/connect/middleware-methodOverride.html

表达:

app.use(express.bodyParser())
app.use(express.methodOverride())

您的html表单:

<form method="POST">
  <input type="hidden" name="_method" value="put">
</form>

这篇关于如何在Express和Node中从html表单发送放置请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆