【问题标题】:WebMethod Not getting called from Jquery in asp.netWebMethod 没有从 asp.net 中的 Jquery 调用
【发布时间】:2014-03-30 18:01:44
【问题描述】:

我试图从 jquery 调用 asp.net webmethod。但它从未被 Jquery 调用。

[WebMethod]
public void AddProductToCart(int productid)
{
   Response.Write(productid.ToString());
   MyShoppingCart usersShoppingCart = new MyShoppingCart();
   String cartId = usersShoppingCart.GetShoppingCartId();
   try
   {
       usersShoppingCart.AddItem(cartId, productid, 1);
   }
   catch (Exception ex)
   {
       throw new Exception(ex.Message);
   }

jquery函数

function d(t) {
        e.ajax({
        url: "productmodel.aspx/AddProductToCart",
        type: "POST",
        data: JSON.stringify(t),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        sucess: function () {
            alert("added to cart successfully");
        }
    })
}

请帮我解决问题。

【问题讨论】:

  • 会发生什么?检查您的控制台,特别是网络选项卡,请求的状态是什么?我们需要更多信息!
  • 我猜 Webmethods 在 .aspx.cs 文件中应该是静态的,可能是您在控制台选项卡中收到 404 not found 异常
  • 使用ScriptMethod Attribute。见WebMethod vs ScriptMethod,方法必须是public static
  • @KundanSinghChouhan 错过了静态并搞砸了......谢谢

标签: jquery webmethod


【解决方案1】:

ASP.NET Web 方法必须是静态方法。

将您的声明更改为:

[WebMethod]
public static void AddProductToCart(int productid)
{
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2014-08-13
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多