当我在带有参数的控制器上调用操作时遇到问题
在控制器中我有以下操作:
[HttpPost]
public ActionResult UpdateData(string month)
{
return Json(new { success = true, message = "Hello world" });
}
视图中的 ajax 调用如下所示:
$.ajax({
url: '@Url.Action("UpdateData")',
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
cache: false,
data: { },
success: function (data) {
if (data.success) {
alert(data.message);
}
},
error: function (xhr) {
alert(xhr.responseText);
}
});
这很好用,但是当我想传递参数时
data: {month:'may'}
我收到错误,该操作从未被调用。我就是没看到。
EDIT :
好的,我只需要将 json 作为字符串传递:
data: "{month:'may'}"
我想我需要更多的睡眠:-)
你必须只需删除内容类型并且better if you 在 URL 中传递控制器名称:
$.ajax({
url: '@Url.Action("UpdateData","YourControllerName")',
dataType: "json",
type: "POST",
cache: false,
data: {month:'may'} ,
success: function (data) {
if (data.success) {
alert(data.message);
}
},
error: function (xhr) {
alert(xhr.responseText);
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)