Canhdongvang Minhanh
Canhdongvang Minhanh

Reputation: 63

mvc.net 4 ajax working with action httpGet not for httpPost

i have two method httpGet and httpPost inside action Login.

when ajax call, it only working with Login httpGet.

i want it working with method Login httpPost.

...thanks for your answer !

this is my code javascript

$(document).ready(function () {
$(".SignIn").delegate(".btnSignIn", "click", function () {
    var UserName = $("#UserName").val();
    var Password = $("#Password").val();
    $.ajax({
        type: "post",
        url: "/Account/Login",
        data: { UserName: UserName, Password: Password },
    })
    .done(function (message) {
        if (message != null && message != "") {
            alert(message);
        }
        else {
            alert('not working');
        }
    });
    return false;
   });
});

this is two method in Account controllers using membership

    //
    // GET: /Account/Login

    [HttpGet]
    [AllowAnonymous]
    public PartialViewResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return PartialView();
    }

    //
    // POST: /Account/Login

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {            
        return Json("aaaa");
    }

Upvotes: 0

Views: 948

Answers (1)

user3559349
user3559349

Reputation:

Your post method is decorated with the [ValidateAntiForgeryToken] attribute so its expecting the taken in the request, but you are not passing this in the ajax method. Ensure your form includes @Html.AntiForgeryToken() and add the following to the ajax data

__RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),

Upvotes: 1

Related Questions