I’m using FormData to upload files. I also want to send an array of other data.
When I send just the image, it works fine. When I append some text to the formdata, it works fine. When I try to attach the ‘tags’ array below, everything else works fine but no array is sent.
Any known issues with FormData and appending arrays?
Instantiate formData:
formdata = new FormData();
The array I create. Console.log shows everything working fine.
// Get the tags
tags = new Array();
$('.tag-form').each(function(i){
article = $(this).find('input[name="article"]').val();
gender = $(this).find('input[name="gender"]').val();
brand = $(this).find('input[name="brand"]').val();
this_tag = new Array();
this_tag.article = article;
this_tag.gender = gender;
this_tag.brand = brand;
tags.push(this_tag);
console.log('This is tags array: ');
console.log(tags);
});
formdata.append('tags', tags);
console.log('This is formdata: ');
console.log(formdata);
How I send it:
// Send to server
$.ajax({
url: "../../build/ajaxes/upload-photo.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (response) {
console.log(response);
$.fancybox.close();
}
});
How about this?
… and, correspondingly, using
json_decodeon server to deparse it. See, the second value of FormData.append can be…The way I see it, your
tagsarray contains objects (@Musa is right, btw; makingthis_tagan Array, then assigning string properties to it makes no sense; use plain object instead), so native conversion (withtoString()) 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: