0

I'm quite new to MVC and I'm trying to call a method int one of my controllers.

<script>
function checkCardNumber() {

    var content = $('#card-number').val();
    var url = "AccountController/CheckRegisteredCard";
    alert("content and URL are set");
    $.post(url, { cardNumber: content }, function (data) {            
        alert(data);
    });
    alert("called outside of Post");
}
</script>

and here's the C# controller side:

    [HttpPost]
    public string CheckRegisteredCard(string cardNumber)
    {            
        if (/*some condition*/)
        {
            return "";
        }            
        else
        {                
            return "some string";
        }
    }

both alerts("content and URL are set", "called outside of post") are being triggered, but the ajax part is not working at all. What am I doing wrong here?

2 Answers 2

2

If your controller class is called AccountController, the url should be /Account/CheckRegisteredCard.

Have you checked the network tab of your browser? I bet there's a 404 error.

If this snippet of javascript is in a .cshtml file, I would recommend using @Url.Action() to get the url string.

Sign up to request clarification or add additional context in comments.

Comments

2

I would recommend using Fiddler and having that open when the ajax call is being made. This will tell you (1) if the call is being made at all or failing immediately, and (2) what kind of response you are receiving from the controller. Also agree with Jason P about how the url should be calling the controller.

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.