【问题标题】:In express, is it possible to POST an array of objects without AJAX?在快递中,是否可以在没有 AJAX 的情况下发布对象数组?
【发布时间】:2016-09-30 00:53:20
【问题描述】:

我正在尝试使用不带 AJAX 的常规表单提交对象数组,但我发现请求正文并没有被解析为对象数组,而是包含许多与对象名称相对应的字段。

我知道当提交一个基元数组时,你只需用相同的名称填充许多输入,它就会被填充;但是,我似乎无法将其应用于复杂的对象。

我的表单代码相当简单:

<div class="col-sm-9">
    <div class="row">
        <div class="col-md-6">
            <div class="form">
                <div class="form-group">
                    <label for="attachment[0].name" class="control-label">Name</label>
                    <input name="attachment[0].name" class="form-control" value="First Name" type="text">
                </div>
                <div class="form-group">
                    <label for="attachment[0].uri" class="control-label">URI</label>
                    <input name="attachment[0].uri" class="form-control" value="First URI" type="text">
                </div>
                <div class="form-group">
                    <label for="attachment[0].description" class="control-label">Description</label>
                    <textarea rows="4" value="First Description" name="attachment[0].description" class="form-control">First Description</textarea>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form">
                <div class="form-group">
                    <label for="attachment[1].name" class="control-label" >Name</label>
                    <input name="attachment[1].name" class="form-control" value="Second Name" type="text">
                </div>
                <div class="form-group">
                    <label for="attachment[1].uri" class="control-label">URI</label>
                    <input name="attachment[1].uri" class="form-control"  value="Second URI" type="text">
                </div>
                <div class="form-group">
                    <label for="attachment[1].description" class="control-label">Description</label>
                    <textarea rows="4" name="attachment[1].description" class="form-control">Second Description</textarea>
                </div>
            </div>
        </div>
    </div>

我制作了一个示例存储库来展示我的问题; https://github.com/xueye/express-form-issue 您可以在其中运行 node server.js,导航到 http://localhost:3000 并提交条目;请求正文将显示在您的控制台中,它应该显示为:

{ name: '',
  type: '',
  'attachment[0].name': 'First Name',
  'attachment[0].uri': 'First URI',
  'attachment[0].description': 'First Description',
  'attachment[1].name': 'Second Name',
  'attachment[1].uri': 'Second URI',
  'attachment[1].description': 'Second Description' }

是否可以按照我尝试的方式发布数据?

【问题讨论】:

    标签: html node.js express


    【解决方案1】:

    当您使用常规表单或 JavaScript 发出 POST 请求时,您正在向服务器发送格式化文本。

    您不能发送数组,但可以发送数组的文本表示形式。

    the data formats supported by forms 都没有提供表示数据数组的标准方法。

    一种模式(由 PHP 引入)使用方括号来描述类似数组的结构。

    这与您使用的类似,只是您有时使用 JavaScript 样式的点表示法。切换到纯粹使用方括号。

    name="attachment[1][uri]"
    

    ……但您需要在服务器上解码该数据格式。

    在 Express 4 中使用:

    var bodyParser = require("body-parser");
    var phpStyleParser = bodyParser.urlencoded({ extended: true })
    

    app.post('/example', phpStyleParser, function(req, res) {
        console.log(req.body);
        res.json(req.body);
    });
    

    【讨论】:

    • 我缺少扩展部分和方括号。这确实有道理——我一直认为附件[索引]是标准化的,但显然不是!
    • 在 PHP 中我这样使用:name="attachment[][uri]"(数组中没有索引号),这在 express 中不起作用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2011-12-25
    • 2022-08-17
    • 2010-11-16
    相关资源
    最近更新 更多