【问题标题】:get response status of 415 Unsupported Media Type REST client获取 415 Unsupported Media Type REST 客户端的响应状态
【发布时间】:2018-03-12 15:55:57
【问题描述】:

我想向 Azure 上运行的 REST API 发出 POST 请求,并且我想通过 POST 请求传递一个 javascript 对象。但响应显示 415 错误代码 Unsupported Media type。我确实尝试将“内容类型”更改为“应用程序/json”,但得到了相同的响应。

componentDidMount() {

const  bodyFormData = new FormData();
bodyFormData.set('id', 30958);
axios({
  method: 'post',
  url: 'https://example./api/example/GetExamplData',
  data: bodyFormData,
  config: { headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    }}
  })
    .then((response) => {console.log(response)}) 
    .catch(error => {console.log( 'the error has occured: ' + error) })
}

【问题讨论】:

  • 不确定,但这是一个快速的镜头:将您的 headers 设置为独立属性(不要将其包裹在 config 中)

标签: javascript reactjs rest axios


【解决方案1】:

通常 REST API 使用 json mediaType,请确保您的服务器。

试试这个:

const  bodyFormData = { "name":"John", "age":30, "city":"New York"};
axios({
  method: 'post',
  url: 'https://example./api/example/GetExamplData',
  data: JSON.stringify(data),
  config: { headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json' 
    }}
  })
    .then((response) => {console.log(response)}) 
    .catch(error => {console.log( 'the error has occured: ' + error) })
}

确保 const bodyFormData = new FormData(); 返回 json。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2020-11-14
    • 2017-11-16
    • 2021-07-16
    相关资源
    最近更新 更多