Search code examples
c#ajaxpostmodel-view-controllerjsgrid

Ajax POST Request not hitting the Controller method


My code works well for GET method. But in case of POST, the action doesn't seem to invoke

I have tried this asp.net MVC with SQL Server

insertItem: function (item) {
                        console.log("items",item);
                        var d = $j.Deferred();
                        $j.ajax({
                            type: "POST",
                            //data: {},
                            //data: { Email: item.Email, FirstName: item.FirstName },
                            data: item,
                            url: '@Url.Action("Create", "User")',
                            //contentType: 'application/json; charset=utf-8',
                            //dataType: "json",
                        }).done(function (response) {
                            console.log("response", response.response)
                            d.resolve(response.response);
                        });

                        return d.promise();
                    }

If i give type as GET the same code functions properly with the GET method in the controller but POST method doesnt work

This is my controller

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UserViewModel item)
        {
        }

POST Method doesn't invoke


Solution

  • Your jQuery code looks incomplete because your .NET code is having anti-forgery request validation but you havnt't sent the forgery token in the post request.

    If you are using some form then you can add forgery token by this method @Html.AntiForgeryToken()

    And by adding another line of code to post the anti-forgery token in the jQuery post request.

    insertItem: function (item) {
                            console.log("items",item);
                            var d = $j.Deferred();
                            var token = $('input[name="__RequestVerificationToken"]').val();
                            $j.ajax({
                                type: "POST",
                                //data: {},
                                //data: { Email: item.Email, FirstName: item.FirstName },
                                data: {
                                        item:item,
                                        __RequestVerificationToken: token, 
                                },
                                url: '@Url.Action("Create", "User")',
                                //contentType: 'application/json; charset=utf-8',
                                //dataType: "json",
                            }).done(function (response) {
                                console.log("response", response.response)
                                d.resolve(response.response);
                            });
    
                            return d.promise();
                        }
    

    If you want to just test out if its due to anti-forgery token then just remove this line of code from the .Net controller (Your code).

    [HttpPost]
    [ValidateAntiForgeryToken]
    

    You can debug by attaching a debugger at the controller and you will be able to see the error caused due to the anti-forgery token.