我有一个带有参数和文件的FormData的Ajax调用POST。 调试该呼叫,它永远不会到达错误或成功,也不会到达控制器。
FormData包含一个结构,该结构的对象映射为'RegisterMerchantInput'
给出全局信息:
const fdata = new FormData();
致电:
var content = {
businessName: $('#business-id').val(),
(...)
};
buildFormData(content);
$.ajax({
type: 'POST',
url: '/Owners/savemerchant',
contentType: false,
processData: false,
cache: false,
data: fdata,
success: function () {
window.location.href = "../Stores/StoresList";
},
error: function (err) {
window.location.href = "../Home/Error";
}
});
要构建表单数据:
function buildFormData(data, parentKey) {
if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File)) {
Object.keys(data).forEach(key => {
buildFormData(data[key], parentKey ? `${parentKey}[${key}]` : key);
});
} else {
const value = data == null ? '' : data;
fdata.append(parentKey, value);
}
}
在控制器中:
[HttpPost]
[Route("savemerchant")]
public async Task<IActionResult> SaveMerchant(RegisterMerchantInput model){...}
现在我尝试指定[FromBody]和[FromForm] 我尝试过 -异步:错误 -contentType:未定义 以及该参数的各种组合。
通话就像无处不在...
注意:在FormData中也有文件,因此我无法在JSON中序列化
编辑: 请求:
时间:
编辑2: 要记录的要发送的FormData是(请注意,数据都是愚蠢的但正确键入的):
businessName: Tester Merchant
vatCode: a
debitorAccount: a
bicCode: a
currencyMandate: a
currencyMandateDate: 2000-01-01
commissionMandate: a
commissionMandateDate: 2000-01-01
city: Avigliano
postalCode: 85101
address: Via Vai 4
name: Bala
surname: Laika
telephone: a
email: bala@laika.com
password: Pa55word1!
country: IT
province: CZ
stores[0][banner]: b
stores[0][description]: b
stores[0][email]: b@b.b
stores[0][website]: b
stores[0][telephone]: b
stores[0][category]: b
stores[0][latitude]: 41.9022448
stores[0][longitude]: 12.456290099999933
stores[0][country]: IT
stores[0][province]: BA
stores[0][city]: b
stores[0][postalCode]: b
stores[0][address]: b
stores[0][terminals][0][store]: b
stores[0][terminals][0][model]: c
stores[0][terminals][0][concept]: c
stores[0][terminals][0][externalCode]: c
stores[0][logo]: (binary)
图像为2438byte。 十分钟后,我的电脑崩溃了,磁盘充满了100%