ajax传formdata类型的数据,通过ajax发送FormData对象和一个附加参数
I have managed to send a FormData object like so:var formData = new FormData();formData.append('file', this.files[0]);$.ajax({url: urlUploadProductsFile,type: 'POST',data: formData,cache: false,conten
I have managed to send a FormData object like so:
var formData = new FormData();
formData.append('file', this.files[0]);
$.ajax({
url: urlUploadProductsFile,
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false
}, 'json');
Now what I want to do is add an additional CustomerId to send to the server. The following won't work:
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');
And I also tried the following variations:
data: { "file": formData, "CustomerId": 2 }, processData: true
data: JSON.stringify({ "file": formData, "CustomerId": 2 })
data: { "file": JSON.stringify(formData), "CustomerId": 2 }
data: { file: formData, CustomerId: 2 }
Any help appreciated.
解决方案
Try:
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');
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)