最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Axios Error with Status Code 415 in Using Get Method - Stack Overflow

programmeradmin0浏览0评论

Good day,

I'm facing this error code using axios.get. The error code is 415. What I want to do is, supply const object to send it to my controller. I tried to debug the controller but it doesn't proceed in my controller (ASP.NET Core Controller).

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.

Good day,

I'm facing this error code using axios.get. The error code is 415. What I want to do is, supply const object to send it to my controller. I tried to debug the controller but it doesn't proceed in my controller (ASP.NET Core Controller).

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.

Share Improve this question asked Feb 20, 2018 at 5:45 jsonGPPDjsonGPPD 1,0375 gold badges17 silver badges32 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

The HTTP status code 415 means Unsupported Media Type. 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,

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.

Can you change your API action FromBody to 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. Parameter Binding in ASP.NET Web API

pass header when calling axios

axios.get("https://api.url.com", {headers: {'Content-Type': 'application/json'}

发布评论

评论列表(0)

  1. 暂无评论