WHEN SENDING JQUERY POST TO MVC CONTROLLER GETTING 404 ERROR

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 781  View(s)
Ratings:
Rate this:

I'm sending from view using jQuery to MVC post action

function DoSomething(passedId) {

                    $.ajax({

                        method: "POST",

                        dataType: 'text',

                        url: '/MyController/SomeAction/',

                        data: { id: passedId }

                    }).done(function (data) {

                        //                       

                    });

                }

And inside MyController

[HttpPost]

public ActionResult SomeAction(int id)

{

       ...

}

  1. Tanuj Kumar

    Post:134

    Points:940
    Re: When sending jQuery post to MVC controller getting 404 error

    You didn't said which version of jquery you are using. Please check jquery version and in case that this version is < 1.9.0 you should instead of

    Method: ”post”

    Use

    Type: ”post”

    this is an alias for method, and according to jquery official documentation you should use type if you're using versions of jQuery prior to 1.9.0.

    function DoSomething(passedId) {   

                        $.ajax({

                            type: "POST",

                            dataType: 'text',                      

                            url: '/MyController/SomeAction/',

                            data: { id: passedId}

                        }).done(function (data) {                                       

                            ...

                        });

                    }

    Tested above code and it works (each request enter inside mvc controller http post SomeAction action).

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!