什么是用快递提交表单的首选方式?

在我的第一个Node项目中使用了Express / Express-资源库,并使用Jade进行模板化。

根据文档默认映射生成。 其中我们可以find:

PUT /forums/:forum -> update 

但是,我看不到提交价值的简单方法。

如何提交创build/更新?

翡翠forms可以很容易地创build和身体parsing器,但如何提交这种forms? 请注意,express-resource定义了PUT方法(不是POST)。

从快速指南 :

当使用PUT等forms的方法时,我们可以使用一个名为_method的隐藏input,它可以用来修改HTTP方法。 要做到这一点,我们首先需要methodOverride中间件,它应该放在bodyParser下面,以便它可以利用它的包含表单值的请求。

所以:

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

并以你的forms:

 <input type="hidden" name="_method" value="put"> 

更新:因为我理解来自提问者的新评论,nrph希望通过使用ajax提交PUT方法的表单。 这是一个使用jQuery的解决scheme:

 // Use this submit handler for all forms in document $(document).on('submit', 'form', function(e) { // Form being submitted var form = e.currentTarget; // Issue an ajax request $.ajax({ url: form.action, // the forms 'action' attribute type: 'PUT', // use 'PUT' (not supported in all browsers) // Alt. the 'method' attribute (form.method) data: $(form).serialize(), // Serialize the form's fields and values success: function() {}, error: function() {} }); // Prevent the browser from submitting the form e.preventDefault(); });