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.

3
  • 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. Commented May 7, 2014 at 10:01
  • What is the Controller name? Commented May 7, 2014 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 - blog.codelab.co.nz/2013/04/29/… and also this - stackoverflow.com/questions/1760607/… Commented May 7, 2014 at 13:04

4 Answers 4

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);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I have used both Get and Post but the problem is still same
1

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

 url: "/HomePage/GetAllCategories/",

Comments

0

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

$.getJSON("/HomePage/GetAllCategories",        
            function(returnData) {
              alert(returnData);           
           });

Comments

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);
    }

7 Comments

Probably be AllowPost in the second Action.
@EhsanSajjad Method Typs is '[HttpPost]' and return type of the method is 'JsonRequestBehavior.AllowGet'. As per my knowledge this is not possible in MVC.
we can return JSON reponse after HttpPost
@EhsanSajjad method was not hitted
pass url this way: url: '@Url.Action("GetAllCategories","HomePage")' i suspect url is passing wrong
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.