【发布时间】:2021-12-27 09:53:42
【问题描述】:
我的 JavaScript 中有一些 AJAX 代码没有显示任何成功或失败警报。
function AttemptHouseViewingAppointment(house) {
var imgOfHouse = $(house).attr("value");
$.ajax({
type: "POST",
url: '@Url.Action("AttemptHouseViewingAppointment", "Viewing")',
dataType: "json",
data: ({
userId: @Model.UserId,
appointmentKey: '@Model.Key',
chosenHouse: imgOfHouse
}),
success: function (data) {
alert(data);
if (data.success) {
alert(data.message);
} else { alert(data.Message) }
},
error: function (xhr) {
alert(xhr.responseText);
}
});
};
当我单击屏幕上的图像时,会调用上述函数。这部分工作正常,因为我在我的 ASP 控制器上设置了一个断点,我可以看到正在调用的相关操作。 C#代码如下:
public ActionResult AttemptHouseViewingAppointment(int userId, string appointmentKey, int chosenHouse)
{
string selecteHouseName = $"./house-code-icons/{chosenHouse}.png";
var house =
_ctx.houses.Where(x => x.HouseID == userId && x.Icon == chosenHouse)
.FirstOrDefault() ?? null;
if(house != null)
{
var member = _ctx.User.FirstOrDefault(x => x.Id.Equals(userId));
_ctx.Appointments.Add(new ViewingModel
{
House = chosenHouse,
UserId = userId
});
_ctx.SaveChanges();
return Json(new { success = true, message = "Appointment Confirmed!" });
}
else
{
return Json(new { success = false, message = "Sorry, a booking has already been made!" });
}
}
即使返回 Json 行被点击并返回到页面,我的页面上也没有弹出警报让用户知道是否成功。如有任何问题,请告诉我。
谢谢
【问题讨论】:
-
您是否检查过浏览器控制台,看看您的控制器返回后是否收到 JS 错误?
标签: javascript c# asp.net ajax model-view-controller