Javascript jQuery formData |列表参数为空
因此,我正在使用表单数据进行ajax调用,我传递的一个参数成功地到达了具有给定值的API端点,但是另一个参数没有,并且始终作为null传递 前端部分:Javascript jQuery formData |列表参数为空,javascript,jquery,.net,ajax,api,Javascript,Jquery,.net,Ajax,Api,因此,我正在使用表单数据进行ajax调用,我传递的一个参数成功地到达了具有给定值的API端点,但是另一个参数没有,并且始终作为null传递 前端部分: $("#approve-registration").click(function (e) { e.preventDefault(); var registration = { RegistrationId: $("#registrationId&qu
$("#approve-registration").click(function (e) {
e.preventDefault();
var registration = { RegistrationId: $("#registrationId").val(), Applications: window.applications };
console.log(registration);
var formData = new FormData();
formData.append('Applications', registration.Applications); //This one is always passed as null
formData.append('RegistrationId', registration.RegistrationId);
$.ajax({
url: "/registration/approve",
type: 'POST',
cache: false,
contentType: false,
processData: false,
data: formData,
success: function (response) {
if (result != null) {
alert("Registration " + result.RegistrationId + " saved sucessfully!");
//location.href = '/registration/index';
} else {
alert("Something went wrong!");
}
}
});
}
注册内容如下:
传递给API终结点的参数:
注册批准模型的结构:
public class RegistrationApproveModel
{
public string RegistrationId { get; set; }
public List<ApplicationModel> Applications { get; set; }
}
您有没有提示为什么应用程序被传递为null?
我要强调的是,另一个参数(RegistrationId)有值。请尝试设置ajax属性
数据类型:“json”,
Ty,但它不起作用。让我强调一下,我可以在一个参数(RegistrationId)中传递值,但不能在另一个参数(应用程序)上传递值。是的,但区别在于一个属性只是字符串,另一个是js对象。您可以尝试将其字符串化formData.append('Applications',JSON.stringify(registration.Applications))
并在服务器上序列化itI没有在后端API上对序列化部分进行编码,但至少在前端添加字符串化没有帮助,因为参数仍然以null到达端点。
public class ApplicationModel
{
public string Name { get; set; }
public List<SettingModel> Settings { get; set; }
}
public class SettingModel
{
public string Name { get; set; }
public string Value { get; set; }
}