2

I got the simple usecase to just send an AJAX request to my controller and receive the result. My current Problem is that the Ajax call returns success with the complete html code of the current page as content and not the return value of my Controller method.

I am a web developer beginner, hints regarding the IIS Express configuration in Visual Studio might also help. Even if this peace of code is part of a larger project.

I know there are a lot of similar questions, but they didn't help at all: javascript ajax call not not hitting controller Asp.net MVC Ajax call not calling controller method

This is my Razor Code:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.Partial("CEinzelkondition", Model)
    <input id="SubmitButton" type="submit" value="Speichern" />
}

This ist my JS code:

function calculateRate() {
            $.ajax({
                type: "POST",
                url: "@Url.Action("CalculateRate", "Marzipan")",
                data: "test",
                dataType: "text",
                contentType: "text",
                success: function (response) {
                    alert("success: " + response);
                },
                failure: function (response) {
                    alert("fail: " + response);
                },
                error: function (xhr, desc, error) {
                    window.alert('description: ' + desc);
                    window.alert('error: ' + error);
                }
            });
        };

The JS function is called by a button in the html partial view.

This is my Controller code:

[HttpPost]
[ValidateAntiForgeryToken]
public string CalculateRate(string data)
{
    data = "success!";
    return data;
}
3
  • Are you sure the what is being returned is your whole web page, and not an error page? Commented Oct 19, 2018 at 14:10
  • I cant see you add antiforgery token during call, it is checked by MVC [ValidateAntiForgeryToken] Commented Oct 19, 2018 at 14:34
  • guess this is the right direction, will test it on monday Commented Oct 19, 2018 at 14:38

1 Answer 1

2

You've specified [ValidateAntiForgeryToken] but you're not posting it in the ajax call. You're just sending a string of the word 'test'.

See here for how...

include antiforgerytoken in ajax post ASP.NET MVC

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

1 Comment

thanks, will test it on monday!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.