请求的资源不支持http方法' GET"在WebAPI中进行POST调用

时间:2017-07-04 07:24:49

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

我正在尝试调用我编写的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);
});

2 个答案:

答案 0 :(得分:2)

问题是因为您在$.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...
});

答案 1 :(得分:0)

尝试按以下方式执行

       $http({

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