javascript - Method not Allowed Error in Jquery Ajax Method used - Stack Overflow

admin2025-04-18  1

I am using jQuery Ajax method with Asp 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.

I am using jQuery Ajax method with Asp 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.

Share edited Sep 12, 2018 at 6:23 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked May 7, 2014 at 9:56 Abhay PrinceAbhay Prince 2,1621 gold badge16 silver badges17 bronze badges 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. – Nanda Commented May 7, 2014 at 10:01
  • What is the Controller name? – Nanda 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./questions/1760607/… – ramiramilu Commented May 7, 2014 at 13:04
Add a ment  | 

4 Answers 4

Reset to default 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);
    }
});

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

 url: "/HomePage/GetAllCategories/",

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

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

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);
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744950243a276289.html

最新回复(0)