我正在尝试通过JQuery AJAX发送空数据。但是,我最终收到一条错误消息,上面写着"错误请求"。我尝试使用单词"null"发送到数据库,但是,这不起作用。数据库允许空值。我可以手动将数据直接添加到数据库中,同时在空列中不输入任何数据。
$.ajax({
method: 'POST',
url: 'http://localhost:58371/api/ScheduleDateAndTime',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
"startDateAndTime": startTotalTimeAndDate,
"endDateAndTime": endTotalTimeAndDate,
"studentId": studentIdSelected,
"staffId": staffIdSelected
}),
success: function (data) {
alert("You have assigned staff member " + staffSelected + " to student " + studentSelected + " on " + datePickerValue + " starting at " + startTime + " and ending at " + endTime);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError + " have you checked that you have selected a date in the calander?n Please ensure all fields have been correctly entered");
}
});
我希望能够在数据库中的"studentId"列上发送空数据。"studentId"是一个变量,它从下拉列表中获取其数据,其中一个可用选项是"none",其ID为"null"。
ASP.NET DTO 模型不允许空值,一旦将其更改为以下内容,我就可以上传空值
public int? studentId { get; set; }
不要发送应该为空的值
var DataToSend = {};
if(startTotalTimeAndDate != undefined)
DataToSend.startDateAndTime= startTotalTimeAndDate;
if(endTotalTimeAndDate!= undefined)
DataToSend.endDateAndTime= endTotalTimeAndDate;
if(studentIdSelected!= undefined)
DataToSend.studentId= studentIdSelected;
if(staffIdSelected!= undefined)
DataToSend.staffId= staffIdSelected;
$.ajax({
method: 'POST',
url: 'http://localhost:58371/api/ScheduleDateAndTime',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(DataToSend),
success: function (data) {
alert("You have assigned staff member " + staffSelected + " to student " + studentSelected + " on " + datePickerValue + " starting at " + startTime + " and ending at " + endTime);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError + " have you checked that you have selected a date in the calander?n Please ensure all fields have been correctly entered");
}
});