在邮递员中,我成功调用了一个对API的请求。
当我在ReactJS中使用fetch()
尝试此操作时,它会返回Internal Server Error (500)
我的提取内容如下:
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: {
...
}
}
邮递员>正文:
{
"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%确定两个身体彼此相等。
我找不到问题所在。你们看到了吗?
答案 0 :(得分:1)
fetch将按原样发送body
,在您的情况下,您发送的是JS对象,而不是JSON数据。
body: { ... }
如果服务器需要JSON数据,则需要将对象转换为JSON字符串,即
body: JSON.stringify({ ... })
P.S。值得注意的是,这可能突出显示了服务器端对无效请求的验证不足,您可能需要解决此问题。