0

How does one post an array of objects containing user data to the server where it can be verified? I am trying to get data from an orderform to pass through to a server to be verified and then displayed on another page I redirect to.

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));
2
  • 1
    JSON.stringify it? You can send it as a buffer and convert it to string less computation in some regards. Commented Jan 6, 2022 at 2:59
  • I generally will run a POST in express, and when it's called, check the body on the client side I will via JS bypass defaults of a form and manually submit it with the body, but know it doesn't need JSON conversion in this format. It's all url encoded/decoded if your project is set properly. :) Commented Jan 6, 2022 at 3:06

1 Answer 1

1

You can use fetch or libraries like axios.

Check this question how to do a POST request with fetch: Fetch: POST JSON data

Example POST request function with axios:

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)

or check POST examples in the axios documentation: https://axios-http.com/docs/post_example

// 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'
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.