2017-07-04 96 views
1

我試圖調用我寫的API,但是當我試圖通過$.ajax調用POST方法時,它會返回一個錯誤:在WebAPI中進行POST調用時,請求的資源不支持http方法「GET」

The requested resource does not support http method 'GET"

當我嘗試通過郵遞員打電話,我得到了想要的結果。在此之上,則$.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

的問題是因爲你在$.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... 
}); 
+0

謝謝。有效。 –

0

嘗試做如下

 $http({ 

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