Categories
JavaScript Answers

How to append array to FormData and send via AJAX with JavaScript?

Spread the love

To append array to FormData and send via AJAX with JavaScript, we create a FormData object.

For instance, we write

const formData = new FormData();
const arr = ["this", "is", "an", "array"];

for (const a of arr) {
  formData.append("arr[]", a);
}

to loop through the arr array items with a for-of loop.

In the loop, we call formData.append with the key and the value to append to the form data object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *