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.

4
  • 1
    What happens? Check your console, specifically the network tab, what is the status of the request? We need more info! Commented Mar 30, 2014 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 Commented Mar 30, 2014 at 18:05
  • Use ScriptMethod Attribute. See WebMethod vs ScriptMethod, and the method must be public static Commented Mar 30, 2014 at 18:09
  • @KundanSinghChouhan missed static and messed up... thanks Commented Mar 30, 2014 at 18:37

1 Answer 1

5

ASP.NET Webmethods must be static methods.

Change your declaration to :

[WebMethod]
public static void AddProductToCart(int productid)
{
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.