【问题标题】:Method not Allowed Error in Jquery Ajax Method used使用的 Jquery Ajax 方法中的方法不允许错误
【发布时间】:2014-06-24 05:42:01
【问题描述】:

我在 Asp.net MVC 3.0 中使用 jQuery Ajax 方法

我的 jQuery 代码是

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

我收到了错误

POST http://localhost:50500/HomePage/GetAllCategories 405(方法不 允许)

我的调试器没有点击这个方法。

【问题讨论】:

  • 尝试在 getAllCategories 方法上方添加 [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]。正在做类似的事情,但我不确定这是否会解决它。
  • 控制器名称是什么?
  • 我不认为这是一个编码问题。这是配置的问题。因为我使用了相同的代码并且无法重现它。但是检查这个解决方案 - blog.codelab.co.nz/2013/04/29/… 和这个 - stackoverflow.com/questions/1760607/…

标签: javascript jquery asp.net ajax asp.net-mvc


【解决方案1】:

您已经在控制器中创建了 GET 方法,并且在 jquery AJAX 调用中将方法类型设置为 POST。

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

【讨论】:

  • Get 和 Post 我都用过,但问题还是一样
【解决方案2】:

只需在网址末尾添加“/”即可:

 url: "/HomePage/GetAllCategories/",

【讨论】:

    【解决方案3】:

    好的,试试这个。我正在使用 getJson 调用来尝试获取相同的数据。

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

    【讨论】:

      【解决方案4】:

      在ajax调用中设置GET类型:

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

      如果想通过 POST 进行,那么:

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

      和行动:

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

      【讨论】:

      • 可能是第二个Action中的AllowPost。
      • @EhsanSajjad 方法类型为“[HttpPost]”,方法的返回类型为“JsonRequestBehavior.AllowGet”。据我所知,这在 MVC 中是不可能的。
      • 我们可以在HttpPost之后返​​回JSON响应
      • @EhsanSajjad 方法未命中
      • 以这种方式传递 url: url: '@Url.Action("GetAllCategories","HomePage")' 我怀疑 url 传递错误
      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2018-03-07
      • 2012-10-26
      • 2021-08-16
      • 2017-07-22
      相关资源
      最近更新 更多