【发布时间】:2018-11-19 16:12:36
【问题描述】:
我正在尝试使用 ajax 调用从视图向控制器发送 2 个参数。当我只使用 1 个参数时,此功能较早时有效,但由于我添加了第二个参数,它不再有效。
带有ajax的Javascript:
function DeleteRole(roleId) {
var confirmation = confirm("Are you sure you want Delete this Role");
if (confirmation) {
$.ajax({
type: "POST",
url: '@Url.Action("Delete_Role", "Admin")',
dataType: 'json',
data: JSON.stringify({
roleId: roleId,
applicationId: $('#AppList').val()
})
}).success(function (response) {
if (response.success) {
alert(response.responseText);
$(".k-grid").data("kendoGrid").dataSource.read();
}else {
alert(response.responseText);
}
}).error(function() {
alert("Error on Deletion");
});
}
}
MVC - 控制器方法(这里根本没有信息)
[HttpPost]
public JsonResult Delete_Role(Guid rowId, Guid applicationId)
{
var users = new UserStore().ReadForAppRole(applicationId, rowId);
if (users.Any())
{
return Json(new { success = false, responseText = "Users's Currently Exist Within this Role" },
JsonRequestBehavior.AllowGet);
}
new RoleStore().RemoveRole(applicationId, rowId);
return Json(new { success = true, responseText = "Role Successfully Deleted" },
JsonRequestBehavior.AllowGet);
}
【问题讨论】:
-
你有一个
rowIdvsroleId变量名需要匹配也不确定如果传递了无效的 guid 会发生什么 - 认为它可能只是作为 null 出现 -
您是否尝试将最终 URL 更改为虚假位置或控制台使用获取全局帖子记录 ajax 请求? @Pete 他的评论也很合适 - 参数也可能是一个问题,但我个人会首先尝试找出帖子是否发生然后继续
-
您正在发送 JSON 字符串,但没有通过内容类型告诉服务器。 OTOH 没有特别需要将其作为 JSON 发送,如果您只是删除 stringify 操作,那么 jQuery 将为您对其进行形式编码,MVC 阅读它应该没有任何问题。
-
不应该删除使用
[HttpDelete]?
标签: javascript ajax asp.net-mvc