【发布时间】:2022-01-06 02:56:18
【问题描述】:
如何将一组包含用户数据的对象发布到可以验证的服务器上? 我正在尝试从订单中获取数据以传递到服务器进行验证,然后显示在我重定向到的另一个页面上。
const addProducts = (ev) => {
ev.preventDefault();
let products = [];
for(let k = 0; k < counter+1; k++) {
let bund = 'Bundled' + k;
let sing = 'Single' + k;
let unit = 'BundledUnits' + k;
let name = 'ProductTitle' + k;
let quant = 'Quantity' + k;
let lPrice = 'ListPrice' + k;
let instruct = 'Instructions' + k;
if(document.getElementById(asin)) {
if(document.getElementById(bund) && document.getElementById(bund).checked) {
if(document.getElementById(unit).value) {
let bundProduct = {
id: k.value,
"Bundled": "true",
"Product": document.getElementById(name).value,
"quantity": document.getElementById(quant).value,
"ListPrice": document.getElementById(lPrice).value,
"Instructions": document.getElementById(instruct).value
};
products.push(bundProduct);
}
}
else if(document.getElementById(sing) && document.getElementById(sing).checked) {
let singProduct = {
id: k.value,
"Bundled": "false",
"Product": document.getElementById(name).value,
"quantity": document.getElementById(quant).value,
"ListPrice": document.getElementById(lPrice).value,
"Instructions": document.getElementById(instruct).value
};
products.push(singProduct);
}
}
console.log(k);
}
let order = sessionStorage.setItem('order', JSON.stringify(products));
【问题讨论】:
-
JSON.stringify 吗?您可以将其作为缓冲区发送,并在某些方面将其转换为无字符串计算。
-
我一般会在 express 中运行一个 POST,当它被调用时,检查客户端的 body 我将通过 JS 绕过表单的默认值并手动将它与 body 一起提交,但知道它没有不需要这种格式的 JSON 转换。如果您的项目设置正确,这一切都是 url 编码/解码的。 :)
标签: javascript node.js express server