在本地主机上获取 API 而不是 运行 端口号
Fetch API not running with port number on localhost
我在 React 中使用 fetch API 遇到了一个奇怪的错误。
TypeError: Failed to execute 'fetch' on 'Window': Failed to parse URL from http://localhost:5000/api/v1/subroute/subroute2/lastXdays?rank=10&pageSize=5¤tPage=0&genre=Something&format=69&sort=Somecriteria
然而,当我从 URL(和 运行 端口 80 上的本地主机服务器)中删除 :5000
部分时,它工作正常。
这是我的获取调用:
const response = await fetch(`${baseUrl}/api/v1/subroute/subroute2/lastXdays?rank=${rank}&pageSize=${pageSize}¤tPage=${currentPage}&genre=${genre}&format=${formatId}&sort=${sort}` , {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'token': token
}
});
return await response.json();
为什么会这样?
我的服务器运行宁在端口 5000 上并且已确认。
您在端口号后面有一个不可见字符,请将其删除。
顺便说一句,如果您将 URL 复制粘贴到 Chrome 中的 DevTools 控制台中,它会将不可见字符显示为一个点。
我在 React 中使用 fetch API 遇到了一个奇怪的错误。
TypeError: Failed to execute 'fetch' on 'Window': Failed to parse URL from http://localhost:5000/api/v1/subroute/subroute2/lastXdays?rank=10&pageSize=5¤tPage=0&genre=Something&format=69&sort=Somecriteria
然而,当我从 URL(和 运行 端口 80 上的本地主机服务器)中删除 :5000
部分时,它工作正常。
这是我的获取调用:
const response = await fetch(`${baseUrl}/api/v1/subroute/subroute2/lastXdays?rank=${rank}&pageSize=${pageSize}¤tPage=${currentPage}&genre=${genre}&format=${formatId}&sort=${sort}` , {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'token': token
}
});
return await response.json();
为什么会这样?
我的服务器运行宁在端口 5000 上并且已确认。
您在端口号后面有一个不可见字符,请将其删除。 顺便说一句,如果您将 URL 复制粘贴到 Chrome 中的 DevTools 控制台中,它会将不可见字符显示为一个点。