Ajax 删除项后返回JSON结果
我正在用MVC3开发一个web应用程序,希望在用户成功删除一个项目后向他返回一条消息 MyWallController方法如下所示:Ajax 删除项后返回JSON结果,ajax,json,asp.net-mvc-3,Ajax,Json,Asp.net Mvc 3,我正在用MVC3开发一个web应用程序,希望在用户成功删除一个项目后向他返回一条消息 MyWallController方法如下所示: [HttpPost] public ActionResult DeleteAlbum(Guid albumId) { try { this.albumService.DeleteAlbum(albumId); return Json(new { success = true, msg = "Album success
[HttpPost]
public ActionResult DeleteAlbum(Guid albumId)
{
try
{
this.albumService.DeleteAlbum(albumId);
return Json(new { success = true, msg = "Album successfully deleted" }, JsonRequestBehavior.AllowGet);
}
catch (FPSException e)
{
return Json(new { success = false, msg = e.Message });
}
catch (Exception)
{
throw new HttpException(500, "Error while deleting album");
}
}
链接:
<a class="open-DeleteAlbumDialog" href="http://localhost:2941/MyWall/DeleteAlbum?albumId=0f49b1ad-8ec1-4fca-b8e2-28bdbf47824e">Delete</a>
然而,post中定义的函数从未被调用,我得到的是一个“资源找不到”。但该项目已成功删除
感谢您提供的各种帮助。您的链接仍然有效。您需要
preventDefault
:
$(function () {
$(document).on('click', '.open-DeleteAlbumDialog', function (e) {
e.preventDefault();
var answer = confirm('Are you sure you want to delete this album?')
if (answer) {
$.post(this.href, function (data) {
if (data.success) {
// do something
} else {
// do something else
}
});
}
});
$(function () {
$(document).on('click', '.open-DeleteAlbumDialog', function (e) {
e.preventDefault();
var answer = confirm('Are you sure you want to delete this album?')
if (answer) {
$.post(this.href, function (data) {
if (data.success) {
// do something
} else {
// do something else
}
});
}
});