0

I tried to make a call from jquery to asp.net webmethod.But it never get invoked from 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

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");
        }
    })
}

Please help me to resolve the issue.

padma2129
  • 49
  • 1
  • 10
  • 1
    What happens? Check your console, specifically the network tab, what is the status of the request? We need more info! – tymeJV Mar 30 '14 at 18:03
  • 1
    I guess Webmethods should be static in .aspx.cs files, may be you are getting 404 not found exception in your console tab – Kundan Singh Chouhan Mar 30 '14 at 18:05
  • Use [ScriptMethod Attribute](http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute(v=vs.110).aspx). See [WebMethod vs ScriptMethod](http://stackoverflow.com/questions/941484/webmethod-vs-scriptmethod), and the method must be `public static` – Sen Jacob Mar 30 '14 at 18:09
  • @KundanSinghChouhan missed static and messed up... thanks – padma2129 Mar 30 '14 at 18:37

1 Answers1

5

ASP.NET Webmethods must be static methods.

Change your declaration to :

[WebMethod]
public static void AddProductToCart(int productid)
{
    ...
Ruskin
  • 5,721
  • 4
  • 45
  • 62
user3770586
  • 51
  • 1
  • 2