mvc中的ajax和事务错误

Posted

技术标签:

【中文标题】mvc中的ajax和事务错误【英文标题】:Error with the ajax and transaction in mvc 【发布时间】:2015-01-11 23:38:10 【问题描述】:

我的ajax是:

<script>
  $( document ).ready(function()
    $('#addtocart').click(function () 
      var size = $('#ddlsize').val();
      var color = $('#ddlcolor').val();
      var id ='@Model.ProductId';
      alert(size + color +id);
      $.ajax(
        url: '@Url.Action("AddTocart", "ShoppingCart")',
        data:                                              
          id:  id,
          size: size,
          color: color,                                           
        ,
        dataType: "html",
        type: 'POST',
        success: function (data) 
          alert("Da them vao gio hang");
        ,
        error: function () 
          alert("Co loi xay ra vui long thu lai");
        
      );
    );
  );
</script>

在我的控制器中

[HttpPost]
public ActionResult AddTocart(int id, string size, string color)

  Product productitem = dbcon.Products.Where(p => p.ProductId == id).SingleOrDefault();
  var cart = ShoppingCart.Getcart(this.HttpContext);
  cart.AddtoCart(productitem, size, color);
  return View();

没有httpget addtocart。当我点击按钮addtocart时,ajax有时会出错,但它会执行addtocart操作并保存在数据库中,有时成功有时错误但不保存数据库,我不知道发生了什么问题?

【问题讨论】:

return View(); 对于 ajax 调用是错误的,你需要使用类似 return Json(new success = true ); 它总是出错或有时@markpsmith,因为我有另一个这样的ajax并返回视图,但它总是成功, 你遇到了什么错误? @ekad,当我调试时,我单击按钮添加到购物车,这会导致错误警报(“Co loi xay ra vui long thu lai”);但它会执行 AddTocart 操作并保存数据库,之后这在 ajax 中几乎成功了。告诉我当我调用它时它总是出错几次 @ekad 你可以去congle1988-001-site1.myasp.net/Product/Details/3查看错误,但是当你尝试几次时它仍然可以执行添加到购物车和保存数据库的功能 【参考方案1】:

因为这个语法

url: '@Url.Action("AddTocart", "ShoppingCart")'

ajax 调用的url 选项将是/ShoppingCart/AddTocart,因为您在控制器代码中调用return View();

[HttpPost]
public ActionResult AddTocart(int id, string size, string color)

    Product productitem = dbcon.Products.Where(p => p.ProductId == id).SingleOrDefault();
    var cart = ShoppingCart.Getcart(this.HttpContext);
    cart.AddtoCart(productitem, size, color);
    return View();

你告诉控制器在插入数据库后打开这个 url:/ShoppingCart/AddTocart,我猜它不存在所以你得到错误,因为你没有 /Views/ShoppingCart/AddTocart.cshtml 或者你没有此方法在 ShoppingCartController 类中没有 [HttpPost] 属性。

public ActionResult AddTocart()


你应该返回json如下

[HttpPost]
public ActionResult AddTocart(int id, string size, string color)

    Product productitem = dbcon.Products.Where(p => p.ProductId == id).SingleOrDefault();
    var cart = ShoppingCart.Getcart(this.HttpContext);
    cart.AddtoCart(productitem, size, color);
    return Json(new  success = true );

并将dataType 选项更改为json

<script>
  $( document ).ready(function()
    $('#addtocart').click(function () 
      var size = $('#ddlsize').val();
      var color = $('#ddlcolor').val();
      var id ='@Model.ProductId';
      alert(size + color +id);
      $.ajax(
        url: '@Url.Action("AddTocart", "ShoppingCart")',
        data:                                              
          id:  id,
          size: size,
          color: color,                                           
        ,
        dataType: "json",
        type: 'POST',
        success: function (data) 
          alert("Da them vao gio hang");
        ,
        error: function () 
          alert("Co loi xay ra vui long thu lai");
        
      );
    );
  );
</script>

【讨论】:

我能理解一点,但在我第一次在 localhost 中运行时出错,之后几乎成功,如果我返回一个视图并且我有这个视图,ekad 是这个错误吗?? 如果你有这个视图:/ShoppingCart/AddTocart.cshtml 并且你有public ActionResult AddTocart 方法,而ShoppingCartController 中没有[HttpPost] 属性,那么你不会得到错误,但这取决于你想在ajax调用成功后做,真的要重定向到/ShoppingCart/AddTocart吗?

以上是关于mvc中的ajax和事务错误的主要内容,如果未能解决你的问题,请参考以下文章

AJAX POST 到 MVC 控制器显示 302 错误

Spring MVC + Ajax 错误 400

弹出mvc控制器的AJAX后请求中出现未知错误

如何使用 jQuery 和 ASP.NET MVC 从 AJAX 调用返回错误消息?

来自 Javascript AJAX 的 ASP.NET MVC 操作调用收到 500 内部服务器错误

AngularJS和Spring MVC的Ajax GET错误