在表达式中,是否可以在没有AJAX的情况下对一组对象进行POST? [英] In express, is it possible to POST an array of objects without AJAX?

查看:129
本文介绍了在表达式中,是否可以在没有AJAX的情况下对一组对象进行POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用没有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] .nameclass =control-label> Name< / label>
< input name =attachment [0] .nameclass =form-controlvalue =First Nametype =text>
< / div>
< div class =form-group>
< label for =attachment [0] .uriclass =control-label> URI< / label>
< input name =attachment [0] .uriclass =form-controlvalue =First URItype =text>
< / div>
< div class =form-group>
< label for =attachment [0] .descriptionclass =control-label>描述< / label>
< textarea rows =4value =First Descriptionname =attachment [0] .descriptionclass =form-control>第一个描述< / textarea>
< / div>
< / div>
< / div>
< / div>
< div class =row>
< div class =col-md-6>
< div class =form>
< div class =form-group>
< label for =附件[1] .nameclass =control-label> Name< / label>
< input name =attachment [1] .nameclass =form-controlvalue =Second Nametype =text>
< / div>
< div class =form-group>
< label for =attachment [1] .uriclass =control-label> URI< / label>
< input name =attachment [1] .uriclass =form-controlvalue =Second URItype =text>
< / div>
< div class =form-group>
< label for =attachment [1] .descriptionclass =control-label>说明< / label>
< textarea rows =4name =attachment [1] .descriptionclass =form-control>第二个描述< / textarea>
< / div>
< / div>
< / div>
< / div>

我已经做了一个示例库,显示了我的问题; https://github.com/xueye/express-form-issue 你可以在哪里运行 node server.js ,导航到 http:// localhost:3000 并提交条目;请求机构将显示在您的控制台中,应显示为:

  {name:'',
类型:'',
'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'}

是否可以POST数据我尝试的方式?

解决方案

当您使用常规表单或JavaScript进行POST请求时,您将发送格式化文本到服务器。



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



支持的数据格式表单使用标准方法来表示数据数组。



由PHP引入的模式使用方括号来描述数组类结构。 / p>

这与您正在使用的类似,除了您有时使用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);
});


I am trying to submit an array of objects using a regular form, without AJAX, and am finding that instead of the request body being parsed into an array of objects it just has many fields corresponding to the names of the objects.

I know that when submitting an array of primitives, you simply fill many inputs with the same name and it will populate; however, I cannot seem to wrap my head around applying this to complex objects.

My form code is fairly straightforward:

<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>

I have made a sample repository demonstrating my issue; https://github.com/xueye/express-form-issue where you can just run node server.js, navigate to http://localhost:3000 and submit the entry; the request body will show up in your console, where it should show up as:

{ 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' }

Is it possible to POST data in the way I am attempting to?

解决方案

When you make a POST request, with a regular form or with JavaScript, you are sending formatted text to the server.

You can't send an array, but you can send a text representation of an array.

None of the data formats supported by forms come with a standard method to represent an array of data.

A pattern (introduced by PHP) uses square brackets to describe array-like structures.

This is similar to what you are using, except you sometimes use JavaScript style dot-notation. Switch to purely using square-brackets.

name="attachment[1][uri]"

… but you need to decode that data format on the server.

To do that in Express 4 use:

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

and

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

这篇关于在表达式中,是否可以在没有AJAX的情况下对一组对象进行POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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