MVC3中的jQuery Ajax POST
当我使用jQuery.ajax和POST方法从数据库中删除一个项时,我得到以下错误:MVC3中的jQuery Ajax POST,jquery,ajax,asp.net-mvc-3,post,Jquery,Ajax,Asp.net Mvc 3,Post,当我使用jQuery.ajax和POST方法从数据库中删除一个项时,我得到以下错误: GET Http://localhost:54010/Admin/Category/Delete?id=77 404 Not Found 类别控制器中的操作: [HttpPost] public ActionResult Delete(int id) { try { db.CategoryRepository.Delete(id); db.Save(); return Json(n
GET Http://localhost:54010/Admin/Category/Delete?id=77 404 Not Found
类别控制器中的操作:
[HttpPost]
public ActionResult Delete(int id) {
try {
db.CategoryRepository.Delete(id);
db.Save();
return Json(new {Result = "OK"});
} catch (Exception ex) {
return Json(new { Result = "ERROR", Message = ex.Message });
}
}
视图:
为什么单击事件不生成POST?您需要通过设置类型参数告诉ajax方法发出POST请求:
$.ajax({
// method: 'POST', <-- remove this
type: 'POST', // <-- add this
url: '@Url.Action("Delete","Category")',
dataType: 'json',
data: { id: '@Model.CategoryModel.Id' },
success: function (data, textStatus, jqXHR) {
console.log("success");
},
error: function () {
alert('error');
}
});
您需要通过设置type参数告诉ajax方法发出post请求:
$.ajax({
// method: 'POST', <-- remove this
type: 'POST', // <-- add this
url: '@Url.Action("Delete","Category")',
dataType: 'json',
data: { id: '@Model.CategoryModel.Id' },
success: function (data, textStatus, jqXHR) {
console.log("success");
},
error: function () {
alert('error');
}
});
$.ajax({
// method: 'POST', <-- remove this
type: 'POST', // <-- add this
url: '@Url.Action("Delete","Category")',
dataType: 'json',
data: { id: '@Model.CategoryModel.Id' },
success: function (data, textStatus, jqXHR) {
console.log("success");
},
error: function () {
alert('error');
}
});