Can I append an array to ‘formdata’ in javascript?

How about this?

formdata.append('tags', JSON.stringify(tags));

… and, correspondingly, using json_decode on server to deparse it. See, the second value of FormData.append can be…

a Blob, File, or a string, if neither, the value is converted to a
string

The way I see it, your tags array contains objects (@Musa is right, btw; making this_tag an Array, then assigning string properties to it makes no sense; use plain object instead), so native conversion (with toString()) won’t be enough. JSON’ing should get the info through, though.

As a sidenote, I’d rewrite the property assigning block just into this:

tags.push({article: article, gender: gender, brand: brand});

Leave a Comment