我已经成功发送了一个 FormData 对象,如下所示:
var formData = new FormData();
formData.append('file', this.files[0]);
$.ajax({
url: urlUploadProductsFile,
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false
}, 'json');
现在我想做的是添加一个额外的 CustomerId
以发送到服务器。以下内容将不起作用:
var formData = new FormData();
formData.append('file', this.files[0]);
$.ajax({
url: urlUploadProductsFile,
type: 'POST',
data: { "file": formData, "CustomerId": 2 },
cache: false,
contentType: false,
processData: false
}, 'json');
我还尝试了以下变体:
data: { "file": formData, "CustomerId": 2 }, processData: true
数据:JSON.stringify({ "file": formData, "CustomerId": 2 })
data: { "file": JSON.stringify(formData), "CustomerId": 2 }
数据:{ 文件:formData,CustomerId:2 }
感谢任何帮助。
最佳答案
尝试:
var formData = new FormData();
formData.append('file', this.files[0]);
formData.append('CustomerId', 2);
/*
note:: appending in form Data will give "csrf token mismatch error".
so better you make a input feild of type hidden with name = CustomerId
and value = 2
*/
$.ajax({
url: urlUploadProductsFile,
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false
}, 'json');
关于jquery - 通过 ajax 发送 FormData 对象和附加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36448724/