【发布时间】:2019-08-10 15:50:17
【问题描述】:
我正在尝试通过 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”。
【问题讨论】:
-
如果您在为该值发送
null时收到 400 响应,那么听起来您的 ASP 逻辑未正确配置为允许空值。在这种情况下,您需要修改 ASP 代码,而不是您的 JS。此外,请确保您发送的是实际的null值,而不是字符串中的单词,例如'null'。 -
我刚刚通过邮递员 REST 客户端尝试过,但没有成功,所以问题出在 asp.net 逻辑中似乎是对的。我在模型中允许为空,这里是代码“public int?staffId { get; set; }”,但是,连接是由 api 完成的。我也添加了api代码
-
很高兴你把它整理好了