【问题标题】:The requested resource does not support http method 'GET" while making a POST call in WebAPI在 WebAPI 中进行 POST 调用时,请求的资源不支持 http 方法“GET”
【发布时间】:2017-07-04 15:48:33
【问题描述】:

我正在尝试调用我编写的 API。但是,当我尝试通过 $.ajax 调用 POST 方法时,它会返回错误:

请求的资源不支持http方法'GET'

当我尝试通过 Postman 拨打电话时,我得到了想要的结果。最重要的是,同一方法的 $.ajax 适用于所有调用。这是我的 API 方法

[HttpPost]
[Route("api/Ticket/GetTicketsAssignedToTechnician/")]        
public List<Ticket> GetTicketsAssignedToTechnician([FromBody]string technicianEmail)
{
    return dbManager.GetTicketsByAssignedTechnician(technicianEmail);
}
postData: function (serviceURL, parameterValue, success, failure, error) {
    $.ajax({
        url: serviceURL,
        method: "POST",
        data: JSON.stringify(parameterValue),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: success,
        failure: failure,
        error: error
    });
}

这里是$.ajax:

Utility.postData(Dashboard.hostURL + "Ticket/GetTicketsAssignedToTechnician/", email, function(data) {
  console.log(data);
}, function(data) {
  console.log("failure." + data.responseText);
}, function(data) {
  console.log("Error." + data.responseText);
});

【问题讨论】:

    标签: jquery ajax asp.net-web-api


    【解决方案1】:

    问题是因为您在 $.ajax 选项中使用了错误的属性名称。这是type,而不是method。因此 jQuery 使用默认值,即GET

    $.ajax({
        url: serviceURL,
        type: "POST", // < change here
        data: parameterValue, // no need to JSON.stringify here, jQuery will do it for you
        // other options...
    });
    

    【讨论】:

      【解决方案2】:

      尝试如下操作

             $http({
      
                  method: "POST",
                  url: serviceURL,
                  contentType: "application/json; charset=utf-8",
                  data: JSON.stringify(parameterValue),
                  dataType: 'JSON'
              })
      

      【讨论】:

        猜你喜欢
        • 2015-10-08
        • 2017-10-17
        • 2016-11-18
        • 2021-12-09
        • 1970-01-01
        • 2012-09-27
        • 2014-01-28
        • 1970-01-01
        • 2018-06-24
        相关资源
        最近更新 更多