简体   繁体   English

使用 Get 方法时出现状态码为 415 的 Axios 错误

[英]Axios Error with Status Code 415 in Using Get Method

Good day,美好的一天,

I'm facing this error code using axios.get.我正在使用 axios.get 遇到此错误代码。 The error code is 415. What I want to do is, supply const object to send it to my controller.错误代码是 415。我想要做的是,提供 const 对象将其发送到我的控制器。 I tried to debug the controller but it doesn't proceed in my controller (ASP.NET Core Controller).我试图调试控制器,但它没有在我的控制器(ASP.NET Core 控制器)中进行。

Here are my sample codes:这是我的示例代码:

// 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);});
}

I hope someone will help me find a solution about this.我希望有人能帮我找到解决方案。

The HTTP status code 415 means Unsupported Media Type. HTTP 状态代码 415 表示不支持的媒体类型。 That is it indicates that the server refuses to accept the request because the payload format is in an unsupported format.即表示服务器拒绝接受请求,因为负载格式是不受支持的格式。

According to MDN Web Docs ,根据MDN 网络文档

The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.格式问题可能是由于请求指示的 Content-Type 或 Content-Encoding,或者是直接检查数据的结果。

Can you change your API action FromBody to FromQuery .您能否将 API 操作FromBody更改为FromQuery Technically that should work.从技术上讲,这应该可行。

Yeah Jaliya is right.是的,贾利亚是对的。 You can also view this docs from Microsoft about Parameter Binding in ASP.NET Web API since you're dealing with API request.由于您正在处理 API 请求,因此您还可以查看 Microsoft 提供的有关 ASP.NET Web API 中的参数绑定的文档。 Parameter Binding in 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM