Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVC3中的jQuery Ajax POST_Jquery_Ajax_Asp.net Mvc 3_Post - Fatal编程技术网

MVC3中的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

当我使用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(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');
    }
});