gpt4 book ai didi

javascript - 获取 415 Unsupported Media Type REST 客户端的响应状态

转载 作者:行者123 更新时间:2023-11-30 14:43:57 25 4
gpt4 key购买 nike

我想向在 Azure 上运行的 REST API 发出 POST 请求,并且我想通过 POST 请求传递一个 javascript 对象。但响应显示 415 错误代码不支持的媒体类型。我确实尝试将“内容类型”更改为“应用程序/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) })
}

最佳答案

通常 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。

关于javascript - 获取 415 Unsupported Media Type REST 客户端的响应状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49239490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com