这是主要问题:
: decodeURI(`https://disease.sh/v3/covid-19/countries/${countryCode}`);
在控制台我得到这个:
App.js:59 GET https://disease.sh/v3/covid-19/countries%E2%80%8B/AI 404
url 仅在执行 fetch(url) 时以某种方式更改,当我 console.log 时 countryCode 似乎很好
在 url 中有这些随机字母 %E2%80%8B 我不想要我如何删除它?
const onCountryChange = async (event) => {
const countryCode = event.target.value;
console.log('country info', countryCode)
const url = countryCode === 'worldwide' ? "https://disease.sh/v3/covid-19/all" : decodeURI(`https://disease.sh/v3/covid-19/countries/${countryCode}`);
await fetch(url)
.then(response => response.json())
.then(data => {
setCountry(countryCode);
setCountryInfo(data);
})
};
最佳答案
%E2%80%8B
是 Unicode Character 'ZERO WIDTH SPACE' (U+200B) 的正确 URL 编码.您共享的代码在斜线之前包含一个不可见的空格:
countries/${countryCode}
^^
关于javascript - 使用插入的 javascript 属性获取 url,但 UTF-8 字符不断出现在控制台中导致 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65466061/