这是我的代码发送数据到服务器,但我总是得到这个错误。
let data = {
"email": "test@gmail.com",
"language": "en",
"country": "en",
"istestentry": false
};
let jsonData = JSON.stringify(data);
let encodedData = encodeURIComponent(jsonData);
let url = "https://example.org/b/receiver/external";
let fetchUrl = url + "?datajsonstr=" + encodedData + "&_=" + (new Date()).valueOf();
let datafetch = fetch(fetchUrl);
const [fetchedData, setFetchedData] = useState('');
async function fetchData(){
const {data} = await axios.post(
datafetch
)
setFetchedData(data)
}
useEffect(() => {
fetchData()
}, [])
const handleSubmit = (event) => {
event.preventDefault();
fetchData()
};
我不知道如何解决这个问题。
您正在尝试发送承诺而不是字符串化数据。let datafetch = fetch(fetchUrl);
给你一个承诺。此外,通过参数发送数据是用get
请求完成的。所以我假设你可能需要做一个get
请求:
let fetchUrl = url + "?datajsonstr=" + encodedData + "&_=" + (new Date()).valueOf();
const [fetchedData, setFetchedData] = useState('');
async function fetchData(){
const {data} = await axios.get(fetchUrl)
setFetchedData(data)
}
(注:get
或post
取决于您的api如何处理请求。你可以选择正确的那个)
我是这样解决的。如果你不想使用Axios,你也可以这样解决。
const [dataDone, setDataDone] = useState(false);
useEffect(() => {
let data = {
"email": "test@gmail.at",
"language": "de",
"country": "de",
"istestentry": false
};
let jsonData = JSON.stringify(data);
let encodedData = encodeURIComponent(jsonData);
let url = "https://org.com;
let fetchUrl = url + "?datajsonstr=" + encodedData + "&_=" + (new
Date()).valueOf();
let datafetch = fetch(fetchUrl);
datafetch.then(data => {
setDataDone(true)
})
}, [])