首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将对象数组发布到Node.js服务器?

如何将对象数组发布到Node.js服务器?
EN

Stack Overflow用户
提问于 2022-01-06 02:56:18
回答 1查看 1.1K关注 0票数 0

如何将包含用户数据的对象数组发布到服务器,以便对其进行验证?我正在尝试从orderform获取数据以传递到服务器,以进行验证,然后显示在我重定向到的另一个页面上。

代码语言:javascript
复制
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));
EN

回答 1

Stack Overflow用户

发布于 2022-01-08 15:17:46

您可以使用fetch或像axios这样的库。

检查以下问题如何使用fetch执行POST请求:Fetch: POST JSON data

使用axios的POST请求函数示例

代码语言:javascript
复制
const sendToServer = async (products) => {
    try {
      // store response in variable if needed
      const res = await axios.post(
        "/your_api/products",
        products  // products you want to post
      );
      // return data in case
      return res.data
    } catch (error) {
      console.log(error);
    }
  }
// implement this function where you need it
sendToServer(products)

或者检查axios文档中的POST示例:https://axios-http.com/docs/post_example

代码语言:javascript
复制
// Send a POST request
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// Send a POST request
axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70601953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档