【问题标题】:How to send my jQuery form to node.js connect-form如何将我的 jQuery 表单发送到 node.js 连接表单
【发布时间】:2012-01-31 19:03:54
【问题描述】:

服务器端,我使用下面的函数(用作 espressjs 中间件)

function objCreation(req, res, next){
  req.form.complete(function(err, fields, files){
    if (err) {
       next(err);
    } else {
       // Set params in request
       obj = {};
       obj.label = fields.label;
       // Check if picture is provided
       if(files.image){
          // Move file to correct dir
          var fs = require("fs");
          var folder = '/var/images/';
          var src = files.image.path;
          var filename = files.image.name;
          var target = folder + filename;

          obj.filename = filename;
          req.obj = obj;

           fs.rename(src, target, function (status) {
             next();
          });
       } else {
          obj.filename = 'nofile';
          req.obj = obj;
          next();
       }
    }
  });
}

基本上,它接收提交的表单的字段。该函数使用“connect-form”node.js 模块。

客户端,我使用输入文本:

<input id="new_obj" type="text" placeholder="New"/>

并且在此输入上点击返回时触发回调:

// Add callback for object creation
$('#new_obj').keypress(function(e){
  if (e.which == 13){
    createObj();
  }
});

createObj函数如下:

function createObj(){
 $.ajax({
   url: "/object/create/",
   type: "POST",
   data: { label : "label" },
   success: function(obj) {
     console.log(obj);
   },
   error: function(jqXHR,status,errorThrown) {
     alert(jqXHR.responseText + '-' + status + '-' + errorThrown);
   }
 });
}

我没有设法让最后一部分以正确的格式发送表单,因此使用服务器方法对其进行处理。我不想更改服务器端,因为它已被其他一些客户端使用(iOS 客户端使用 ASIFormDataRequest 发送表单)。

我目前正在查看 ajaxForm jQuery 插件,看看是否有帮助。

【问题讨论】:

  • 你需要使用multipart

标签: javascript jquery node.js express


【解决方案1】:

我设法通过使用不再使用 connect-form(已弃用)但改用 formidable 的最新版本 expressjs 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多