javascript - 使用插入的 javascript 属性获取 url,但 UTF-8 字符不断出现在控制台中导致 404

标签 javascript node.js reactjs

这是主要问题:

: 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%8BUnicode Character 'ZERO WIDTH SPACE' (U+200B) 的正确 URL 编码.您共享的代码在斜线之前包含一个不可见的空格:

countries​/${countryCode}
        ^^

关于javascript - 使用插入的 javascript 属性获取 url,但 UTF-8 字符不断出现在控制台中导致 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65466061/

相关文章:

reactjs - 如何测试子组件从父组件的 getChildContext() 获取正确的上下文?

javascript - 使用ajax请求更新数据表

JavaScript - 检测正零和负零之间的差异

templates - 带布局的渲染 Jade 模板(不含 express )

node.js - 如何在使用 axios 重定向后获取着陆页 URL

javascript - 如何在 React 中缓存图片?

javascript - 在多个 <script> 标签之间分配代码

javascript - 如何在 React Native 中只渲染一次组件

javascript - 在 JavaScript 中人性化字符串

node.js - 休息 API : Should I associate the access token with a user in my database