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