【发布时间】:2012-12-11 04:22:37
【问题描述】:
我正在使用 FormData 上传文件。我还想发送一组其他数据。
当我只发送图像时,它工作正常。当我将一些文本附加到表单数据时,它工作正常。当我尝试在下面附加“标签”数组时,其他一切正常,但没有发送数组。
FormData 和附加数组有任何已知问题吗?
实例化formData:
formdata = new FormData();
我创建的数组。 Console.log 显示一切正常。
// 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);
我如何发送它:
// 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();
}
});
【问题讨论】:
-
为什么要向数组添加属性?改为使用对象。
-
我怀疑有 PHP 背景。数组在 Javascript 中并不是这样工作的。
-
就是这样 :) 我将使用一个对象。
标签: javascript jquery arrays multipartform-data