gpt4 book ai didi

javascript - Fetch 返回一个内部服务器错误,而 Postman 接受它

转载 作者:行者123 更新时间:2023-12-02 23:27:36 31 4
gpt4 key购买 nike

在 Postman 中,我成功调用了对 API 的请求。

当我在 ReactJS 中使用 fetch() 尝试此操作时,它会返回一个内部服务器错误 (500)

我的 Fetch 看起来像:

const FetchData = async (url , request) => {
return await fetch(url, request)
.then(async function(res) {
if (res.ok)
console.log("OK: " + await res.json())
else
console.log("NOT OK: " + await res.text())
});
}

请求:

{
method: "POST",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: {
...
}
}

postman >正文:

{
"value": {
"RequestType":1,
"Dependencies":[],
"Priority":1,
"RequestLocation":"some string",
"RequestJSON": "some string",
"NoCache":true,
"PostNow":true,
"Authentication": {
"Email":"string",
"Device": {
"Name":"string",
"UniqueDeviceId":"string",
"AppVersion":"string",
"AppType":int,
"Devicestatus":int
}
}
}
}

我 100% 确定两个实体彼此相等。

我无法找出问题所在。大家看到了吗?

最佳答案

fetch将按原样发送body,在您的情况下,您发送的是 JS 对象,而不是 JSON 数据,即

body: { ... }

如果服务器需要 JSON 数据,那么您需要将对象转换为 JSON 字符串,即

body: JSON.stringify({ ... })

P.S.值得注意的是,这可能突出表明您的服务器端缺乏对无效请求的验证,您可能需要修复该问题。

关于javascript - Fetch 返回一个内部服务器错误,而 Postman 接受它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668390/

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