![](/img/trans.png)
[英]Axios post method: There is error Error: Request failed with status code 405
[英]Axios Error with Status Code 415 in Using Get Method
美好的一天,
我正在使用 axios.get 遇到此錯誤代碼。 錯誤代碼是 415。我想要做的是,提供 const 對象將其發送到我的控制器。 我試圖調試控制器,但它沒有在我的控制器(ASP.NET Core 控制器)中進行。
這是我的示例代碼:
// Class that I want to supply
public class User{
public int? Name {get;set;}
public DateTime? Bday {get;set;}
}
// My Controller
public async Task<IActionResult> GetUsers([FromBody] User user){
// Do something here
}
// My js file axios is already imported and working
async searchUser(){
const user = {
Name: name,
Bday: bday
}
await axios.get(`/SomePage/GetUsers/`,user).then(response=>{
// do something here
}.catch(error=>{console.log(error);});
}
我希望有人能幫我找到解決方案。
HTTP 狀態代碼 415 表示不支持的媒體類型。 即表示服務器拒絕接受請求,因為負載格式是不受支持的格式。
根據MDN 網絡文檔,
格式問題可能是由於請求指示的 Content-Type 或 Content-Encoding,或者是直接檢查數據的結果。
您能否將 API 操作FromBody
更改為FromQuery
。 從技術上講,這應該可行。
是的,賈利亞是對的。 由於您正在處理 API 請求,因此您還可以查看 Microsoft 提供的有關 ASP.NET Web API 中的參數綁定的文檔。 ASP.NET Web API 中的參數綁定
調用 axios 時傳遞標頭
axios.get("https://api.url.com", {headers: {'Content-Type': 'application/json'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.