[英]Fetch: POST JSON data in react.js
I'm trying to POST a JSON object using fetch.我正在尝试使用 fetch 发布一个 JSON 对象。 On the client side I see all the data, In the API I get the object with NULL values
在客户端,我看到了所有数据,在 API 中,我得到了具有 NULL 值的对象
That's my call:这是我的电话:
const onSubmit = async () => {
console.log(searchCases);
// handleFormChange();
try {
await fetch("/api/cases/create_search", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
searchCases,
}),
}).then(async (response) => {
if (response.ok) {
//let byteArray = await response.arrayBuffer();
let byteArray = await response;
let data = byteArray;
setNewData(data);
console.log(data);
}
});
} catch (e) {
console.error(
'Error: An error was detected in the function "fetch" /api/cases/create_search_case OR in msgpk:',
e
);
}
I'm not sure why do you have to use async/await in side then()
.我不确定为什么你必须在
then()
旁边使用 async/await 。 You can try this:你可以试试这个:
const onSubmit = async () => {
console.log(searchCases);
// handleFormChange();
try {
await fetch("/api/cases/create_search", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
searchCases,
}),
}).then(response => {
let byteArray = response;
let data = byteArray;
setNewData(data);
console.log(data);
}
);
} catch (e) {
console.error(
'Error: An error was detected in the function "fetch" /api/cases/create_search_case OR in msgpk:',
e
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.