1

I am using jQuery Ajax method with Asp.net MVC 3.0

My jQuery Code is

$.ajax({
       type: "POST",
       url: "/HomePage/GetAllCategories",
       contentType: "application/json; charset=utf-8",                
       dataType: 'json',
       success: function (result) {
          alert(result);
    }
});

And my Action Method is

public JsonResult GetAllCategories()
{
     return Json(null, JsonRequestBehavior.AllowGet);
}

I am getting the Error

POST http://localhost:50500/HomePage/GetAllCategories 405 (Method Not Allowed)

My debugger is not hitting this method.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Abhay Prince
  • 1,336
  • 10
  • 14
  • Try adding this, [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] above the getAllCategories method. Woring on something similar, am not really sure if this will fix it though. – Nanda May 07 '14 at 10:01
  • What is the Controller name? – Nanda May 07 '14 at 10:09
  • I dont think this is a coding problem. It is a problem at configuration. Because I used your same code and was not able to repro it. But check this solution - http://blog.codelab.co.nz/2013/04/29/405-method-not-allowed-using-asp-net-web-api/ and also this - http://stackoverflow.com/questions/1760607/asp-net-mvc-got-405-error-on-http-delete-request – ramiramilu May 07 '14 at 13:04

4 Answers4

1

You have created GET method in the controller and you have set method type as POST in your jquery AJAX call.

$.ajax({
       type: "GET",
       url: "/HomePage/GetAllCategories",
       contentType: "application/json; charset=utf-8",                
       dataType: 'json',
       success: function (result) {
          alert(result);
    }
});
Jayesh Goyani
  • 10,535
  • 11
  • 24
  • 48
1

Just add "/" on the end of the URL:

 url: "/HomePage/GetAllCategories/",
Louis Michael
  • 358
  • 1
  • 12
0

Set type GET in the ajax call:

$.ajax({
       type: "GET",
       url: '@Url.Action("GetAllCategories","HomePage")' ,
       contentType: "application/json; charset=utf-8",                
       dataType: 'json',
       success: function (result) {
          alert(result);
    }
});

and action:

[HttpGet]
public JsonResult GetAllCategories()
{
     return Json(null, JsonRequestBehavior.AllowGet);
}

If want to do via POST then:

$.ajax({
           type: "POST",
           url: '@Url.Action("GetAllCategories","HomePage")' ,
           contentType: "application/json; charset=utf-8",                
           dataType: 'json',
           success: function (result) {
              alert(result);
        }
    });

and action:

    [HttpPost]
    public JsonResult GetAllCategories()
    {
         return Json(null, JsonRequestBehavior.AllowGet);
    }
Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146
0

Okay, try this. I am using a getJson call to try and fetch the same data.

$.getJSON("/HomePage/GetAllCategories",        
            function(returnData) {
              alert(returnData);           
           });
Nanda
  • 988
  • 2
  • 15
  • 33