Javascript 方法不允许在使用的Jquery Ajax方法中出现错误
我在Asp.NETMVC3.0中使用jQueryAjax方法 我的jQuery代码是Javascript 方法不允许在使用的Jquery Ajax方法中出现错误,javascript,jquery,asp.net,ajax,asp.net-mvc,Javascript,Jquery,Asp.net,Ajax,Asp.net Mvc,我在Asp.NETMVC3.0中使用jQueryAjax方法 我的jQuery代码是 $.ajax({ type: "POST", url: "/HomePage/GetAllCategories", contentType: "application/json; charset=utf-8", dataType: 'json', success: function (result) {
$.ajax({
type: "POST",
url: "/HomePage/GetAllCategories",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
我的行动方法是
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
我发现了错误
405号岗位(方法不适用)
允许的)
我的调试器没有使用此方法。您已经在控制器中创建了GET方法,并且在jquery AJAX调用中将方法类型设置为POST
$.ajax({
type: "GET",
url: "/HomePage/GetAllCategories",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
在ajax调用中设置typeGET:
$.ajax({
type: "GET",
url: '@Url.Action("GetAllCategories","HomePage")' ,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
和行动:
[HttpGet]
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
如果要通过POST执行此操作,则:
$.ajax({
type: "POST",
url: '@Url.Action("GetAllCategories","HomePage")' ,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
和行动:
[HttpGet]
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
好的,试试这个。我正在使用getJson调用尝试获取相同的数据
$.getJSON("/HomePage/GetAllCategories",
function(returnData) {
alert(returnData);
});
只需在URL末尾添加“/”:
url: "/HomePage/GetAllCategories/",
尝试将此[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]添加到getAllCategories方法上方。正在进行类似的工作,但我不确定这是否会解决它。控制器名称是什么?我不认为这是一个编码问题。这是一个配置问题。因为我使用了你的相同代码,无法重新编程。但请检查此解决方案-以及此解决方案-我同时使用了Get和Post,但问题仍然相同。在第二个操作中,可能是AllowPost。@EhsanSajjad方法类型为“[HttpPost]”,方法的返回类型为“JsonRequestBehavior.AllowGet”。据我所知,这在MVC中是不可能的。我们可以在之后返回JSON响应HttpPost@EhsanSajjad方法未按此方式命中传递url:url:“@url.Action(“GetAllCategories”,“HomePage”)”我怀疑url传递错误