Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9057419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:27:01+00:00 2026-06-16T14:27:01+00:00

I am trying to call a method with jQuery on a button click.This is

  • 0

I am trying to call a method with jQuery on a button click.This is waht I have so far:

$("a.AddToCart").click(function () {
            var link = document.URL;

            $.ajax({
                 type:"POST",
                 url: "/Account/AddToCartHack",
                 data: {url : link},
                 contentType: "application/json; charset=utf-8",
                 dataType: "json"
            });
        });


[WebMethod]
    public void AddToCartHack(string url)
    {
        GeneralHelperClass.URL = url;
    }

What I am trying to do here is when I click the link with class add to cart I want to call the method AddToCartHack and pass it the curent URL as a parameter.
I must be doing something wrong because it seems that AddToCartHack is not being called.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:27:03+00:00Added an answer on June 16, 2026 at 2:27 pm

    There are quite a lot of issues with your code, I don’t know where to start. So let’s conduct an interactive refactoring session with you (if you don’t care about my comments but just want to see the final and recommended solution, just scroll down at the end of answer).

    First: you don’t seem to be canceling the default action of the anchor by returning false from the .click() event handler. By not doing this you are actually leaving the browser perform the default action of the anchor (which as you know for an anchor is to redirect to the url that it’s href attribute is pointing to. As a consequence to this your AJAX call never has the time to execute because the browser has already left the page and no more scripts are ever running). So return false from the handler to give your AJAX script the possibility to execute and stay on the same page:

    $("a.AddToCart").click(function () {
        var link = document.URL;
    
        $.ajax({
            type:"POST",
            url: "/Account/AddToCartHack",
            data: {url : link},
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    
        return false;
    });
    

    Second: You have specified contentType: 'application/json' request header but you are not sending any JSON at all. You are sending a application/x-www-form-urlencoded request which is the default for jQuery. So get rid of this meaningless parameter in your case:

    $("a.AddToCart").click(function () {
        var link = document.URL;
    
        $.ajax({
            type:"POST",
            url: "/Account/AddToCartHack",
            data: {url : link},
            dataType: "json"
        });
    
        return false;
    });
    

    Third: You have specified that your server side code will return JSON (dataType: 'json') but your server side code doesn’t return anything at all. It’s a void method. In ASP.NET MVC what you call C# method has a name. It’s called a controller action. And in ASP.NET MVC controller actions return ActionResults, not void. So fix your contoller action. Also get rid of the [WebMethod] attribute – that’s no longer to be used in ASP.NET MVC

    public class AccountController: Controller
    {
        public ActionResult AddToCartHack(string url)
        {
            GeneralHelperClass.URL = url;
            return Json(new { success = true });
        }
    }
    

    Fourth: you have hardcoded the url to the controller action in your javascript instead of using server side helpers to generate this url. The problem with this is that your code will break if you deploy your application in IIS in a virtual directory. And not only that – if you decide to modify the pattern of your routes in Global.asax you will have to modify all your scripts because the url will no longer be {controller}/{action}. The approach I would recommend you to solve this problem is to use unobtrusive AJAX. So you would simply generate the anchor using an HTML helper:

    @Html.ActionLink(
        "Add to cart", 
        "AddToCartHack", 
        "Account", 
        null, 
        new { @class = "AddToCart" }
    )
    

    and then unobtrusively AJAXify this anchor:

    $('a.AddToCart').click(function () {
        var link = document.URL;
    
        $.ajax({
            type: 'POST',
            url: this.href,
            data: { url : link }
        });
    
        return false;
    });
    

    Fifth: You seem to be employing some document.URL variable inside your .click() handler. It looks like some global javascript variable that must have been defined elsewhere. Unfortunately you haven’t shown the part of the code where this variable is defined, nor why is it used, so I cannot really propose a better way to do this, but I really feel that there’s something wrong with it. Or oh wait, is this supposed to be the current browser url??? Is so you should use the window.location.href property. Just like that:

    $('a.AddToCart').click(function () {
        $.ajax({
            type: 'POST',
            url: this.href,
            data: { url : window.location.href }
        });
    
        return false;
    });
    

    or even better, make it part of the original anchor (Final and recommended solution):

    @Html.ActionLink(
        "Add to cart", 
        "AddToCartHack", 
        "Account", 
        new { url = Request.RawUrl }, 
        new { @class = "AddToCart" }
    )
    

    and then simply:

    $('a.AddToCart').click(function () {
        $.ajax({
            type: 'POST',
            url: this.href
        });
    
        return false;
    });
    

    Now that’s much better compared to what we had initially. Your application will now work even with javascript disabled on the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find the jQuery equivalent of this JavaScript method call: document.addEventListener('click', select_element,
I'm trying to call an asp.net method using jquery/ajax from a button click on
I'm trying to call a Page Method using a jQuery 'attached' event function, in
I have been trying to call JQuery code from Button1 but whenever I click
I have been trying to call filter css method from jquery for IE, but
I am trying to call a jquery function on clicking a button in the
I'm trying to call a method from another class with a simple button in
I'm trying to link my website with Facebook. If I call the method in
I am trying to call a Web Method of service from a Jquery Ajax
I am trying to call an ASP.NET MVC actionMethod via the JQuery ajax method.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.