Php 如何在NodeJS';要求
我使用的API要求我在表单字段中放置一个数组。给出的示例是用PHP编写的,但我使用的是NodeJS。API期望其中一个字段是数组,我很难弄清楚如何做到这一点 PHP示例如下所示:Php 如何在NodeJS';要求,php,arrays,node.js,form-data,node-request,Php,Arrays,Node.js,Form Data,Node Request,我使用的API要求我在表单字段中放置一个数组。给出的示例是用PHP编写的,但我使用的是NodeJS。API期望其中一个字段是数组,我很难弄清楚如何做到这一点 PHP示例如下所示: $request->buildPostBody(array( 'reference' => array( 'line1' => 'Soundboard Setup', 'line2' => 'Thank you for the order', 'line3' => 'Our ref
$request->buildPostBody(array(
'reference' => array(
'line1' => 'Soundboard Setup',
'line2' => 'Thank you for the order',
'line3' => 'Our reference is: 3993029/11BD'
),
'lines' => array(
array('amount' => 50,
'amount_desc' => 'panels',
'description' => 'Sound buttons',
'tax_rate' => 21,
'price' => 5.952
),
array('amount' => 1,
'amount_desc' => '',
'description' => 'Wooden case',
'tax_rate' => 21,
'price' => 249
),
array('amount' => 10,
'amount_desc' => 'hours',
'description' => 'Support',
'tax_rate' => 6,
'price' => 62.5
),
array('description' => 'This is a textline'))
));
var formData = {
reference: {'line1':'something'}, // <- this should work now
lines: [{ // <- this is still an array of objects [{}], as in PHP too, it's a nested array
amount: amount,
'amount_desc': 'amount_desc',
'description': 'description',
'tax_rate': 0,
'price': 100
}]
};
在NodeJS中,我尝试了以下方法(除其他外):
我怎样才能最好地把这个圆钉子塞进那个方孔中?在PHP中,数组可以是数组,也可以是关联数组,它们本质上是JavaScript中的对象 指定 可以使用array()语言构造来创建。它接受任意数量的逗号分隔键=>值对作为参数
array(
key => value,
key2 => value2,
key3 => value3,
...
)
在JavaScript中,上述内容本质上只是一个对象
{
key: 'value',
key2: 'value2'
key3: 'value3'
...
}
它不会是一个封装在数组[{…}]
中的对象,这是您使用引用所做的操作
因此,要模拟PHP结构,您的数据应如下所示:
$request->buildPostBody(array(
'reference' => array(
'line1' => 'Soundboard Setup',
'line2' => 'Thank you for the order',
'line3' => 'Our reference is: 3993029/11BD'
),
'lines' => array(
array('amount' => 50,
'amount_desc' => 'panels',
'description' => 'Sound buttons',
'tax_rate' => 21,
'price' => 5.952
),
array('amount' => 1,
'amount_desc' => '',
'description' => 'Wooden case',
'tax_rate' => 21,
'price' => 249
),
array('amount' => 10,
'amount_desc' => 'hours',
'description' => 'Support',
'tax_rate' => 6,
'price' => 62.5
),
array('description' => 'This is a textline'))
));
var formData = {
reference: {'line1':'something'}, // <- this should work now
lines: [{ // <- this is still an array of objects [{}], as in PHP too, it's a nested array
amount: amount,
'amount_desc': 'amount_desc',
'description': 'description',
'tax_rate': 0,
'price': 100
}]
};
下面将解释此错误:
基本上表单数据
只取简单对象大概只有1层深
试试这个:
var formData = {
'reference.line1': 'something',
'lines[0].amount': amount,
'lines[0].amount_desc': 'amount_desc',
'lines[0].description': 'description',
'lines[0].tax_rate': 0,
'lines[0].price': 100,
};
有趣的是,如果我尝试从节点得到一个奇怪的错误:node_modules/request/node_modules/combined stream/node_modules/delayed stream/lib/delayed_stream.js:33 source.on('error',function(){});^TypeError:Object#没有方法“on”,感谢您澄清了PHP数组映射到js objects@laggingreflect的方式。我想我现在只需要弄清楚如何在NodeJSY中将数组或对象编码到formdata字段中。您的更新是正确的解决方案。我同时得到了同样的结果,只是来这里回答我自己的问题,看到你也到了那里。我认为您应该删除“因此您的数据应该与此类似”部分。
var formData = {
'reference.line1': 'something',
'lines[0].amount': amount,
'lines[0].amount_desc': 'amount_desc',
'lines[0].description': 'description',
'lines[0].tax_rate': 0,
'lines[0].price': 100,
};