【问题标题】:Fetch API not running with port number on localhost [closed]获取 API 未在本地主机上使用端口号运行 [关闭]
【发布时间】:2021-03-26 21:27:31
【问题描述】:

我在使用 React 中的 fetch API 时遇到了一个奇怪的错误。

TypeError: 无法在 'Window' 上执行 'fetch': 无法从 http://localhost:5000​/api/v1/subroute/subroute2/lastXdays?rank=10&pageSize=5&currentPage=0&genre=Something&format= 解析 URL 69&sort=一些标准

但是,当我从 URL 中删除 :5000 部分(并在端口 80 上运行 localhost 服务器)时,它工作正常。

这是我的 fetch 调用:

  const response = await fetch(`${baseUrl}​/api/v1/subroute/subroute2/lastXdays?rank=${rank}&pageSize=${pageSize}&currentPage=${currentPage}&genre=${genre}&format=${formatId}&sort=${sort}` , {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'token': token
    }
  });
  return await response.json();

为什么会这样?

我的服务器在 5000 端口上运行,并且已确认。

【问题讨论】:

  • 你能显示 baseUrl 变量吗?
  • 或删除令牌?
  • @ggorlen 检查编辑。
  • @iwaduarte 删除令牌不能是“解决方案”,baseUrl 显然是(来自控制台日志)http://localhost:5000
  • @4esn0k 是的,我看到了那个看不见的角色。是的,那是在仅在浏览器控制台中可见的提取代码中。那拯救了我的一天!也许你可以考虑把它作为一个答案,我会接受投票

标签: javascript reactjs fetch-api


【解决方案1】:

端口号后面有一个隐形字符,将其删除。 顺便说一句,如果你将 URL 复制粘贴到 Chrome 的 DevTools 控制台中,它会将不可见字符显示为一个点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 2014-02-12
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    相关资源
    最近更新 更多