【问题标题】:POST Request with Jquery's $.post(), 405 Method Not Allowed使用 Jquery 的 $.post() 的 POST 请求,不允许使用 405 方法
【发布时间】:2019-02-15 10:20:11
【问题描述】:

我在 C# 控制器中设置了一个异步任务,如下所示:

命名空间 MyApp.Api {

public class TimeAllocationController : BaseApiController
{
    [Route("api/timeallocation")]
    [HttpPost]
    public async Task<ActivityValidationResult> Post(string id)
    {
        //logic here...
    }

理想情况下,我想使用 $.post() 方法在 JQuery 中传递整个有效负载,但我不断收到 405 method not allowed if

我尝试在有效负载中传递 C# 的 Post() 字符串 ID。我只能这样传递:

$.post('/api/timeallocation/' + categoryId...

我不能像这样传递它:

$.post('/api/timeallocation?id=' + categoryId...

上述两个我都不想做,只需在 JS 文件中设置一个带有 id 和所有其他必需属性的 payload 变量,然后调用 $.post() 即可。

至于出现405错误的其他可能原因,我已经验证不是因为身份验证的原因。

这里有什么我忽略的地方吗?

【问题讨论】:

  • 您需要将路由配置更改为[Route("api/timeallocation/{id}")] 并执行$.post('/api/timeallocation/' + categoryId 以从Ajax 调用

标签: c# jquery .net post .net-4.6


【解决方案1】:

如果您想使用来自 jquery 的有效负载调用它,您应该使用带有 [FromBody] 属性的 Post 方法,如下所示:

public class TimeAllocationController : BaseApiController
{
    [Route("api/timeallocation")]
    [HttpPost]
    public async Task<ActivityValidationResult> Post([FromBody] string id)
    {
        //logic here...
    }

See the documentation

然后你可以用

调用它
$.ajax({
    url: "/api/timeallocation/",
    dataType: "json",
    type: "POST",
    data: {
        id: categoryId
    }
});

【讨论】:

  • 我不知道[FromBody]。无论是您的答案还是此处发布的其他人,似乎我无法获得 405method 错误的唯一方法是如果我在控制器中执行 Post(string id) 并且在 JS 中的路由为 $.post('api/timeallocation/' + categoryId... 。任何使用FromUriFromBody、查询字符串(即?id=categoryId)总是返回405。我在这里忽略了什么?
  • 你能分享你是如何做这个帖子的完整代码吗?您在问题中提到您正在传递多个属性,所以这个答案可能会有所帮助:stackoverflow.com/a/37518529/95499。基本上在做data: JSON.stringify(payload),
  • 其他要考虑的事情,但我不知道,因为它不在您提供的代码 sn-p 中;你启用CORS了吗? stackoverflow.com/q/26649361/95499
【解决方案2】:

你应该打电话

$.post('/api/timeallocation/', categoryId)

或者你可以添加id参数的[FromUri]属性并调用

$.post('/api/timeallocation?id' + categoryId)

【讨论】:

    【解决方案3】:
    public class TimeAllocationController : BaseApiController
    {
        [Route("api/timeallocation")]
        [HttpPost]
        public async Task<ActivityValidationResult> Post(JObject json)
        {
            string id = json["id"];
            //login here
        }
    }
    
    or
    
    public class TimeAllocationController : BaseApiController
    {
        [Route("api/timeallocation")]
        [HttpPost]
        public async Task<ActivityValidationResult> Post(dynamic json)
        {
            string id = json.id ?? "";
            //login here
        }
    }
    
    $.ajax({
        url: "/api/timeallocation/",
        dataType: "json",
        type: "POST",
        data: {
            id: categoryId
        }
    });
    

    【讨论】:

      【解决方案4】:

      我能够在我的控制器中使用以下解决方案:

      public async Task<ActivityValidationResult> Post(string id, [FromBody] TimeAllocationActivity payload)
      

      其中payload 处理TimeAllocationActivity 属性。请注意,我确实必须创建 TimeAllocationActivity 模型,因为它以前不存在。

      在 JS 端,我创建了 payload 变量,然后像这样设置请求:

      processCheckAjax = $.ajax({
          url: "/api/timeallocation/" + categoryId,
          dataType: "json",
          type: "POST",
          data: JSON.stringify(payload)
      

      我确实觉得很奇怪,我仍然必须将categoryId 附加到路由并且它不能包含在有效负载中。

      【讨论】:

        猜你喜欢
        • 2016-02-15
        • 2014-04-18
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        • 2021-11-24
        • 2014-05-23
        • 2012-10-26
        • 2017-01-27
        相关资源
        最近更新 更多