添加到获取请求的相对路径
Relative path being added to fetch request
我正在使用 create-react-app
和 react-router-dom 4
制作 React 应用程序。我的问题是,当我加载默认“/”以外的 URL 时,相对路径会添加到正在发出的请求中。
例如,如果我在 url 搜索栏中输入 mysite.com/templates
,我的提取请求 fetch("templates/item.json")
请求 url: mysite.com/templates/templates/item.json
但如果我从使用来自 react-router-dom 的 Link
组件到 /templates
的主页不是这种情况。
有什么方法可以避免将相对路径添加到获取请求之前?
fetch
将加载相对于您打开的 url 的路径,如果您不以 /
开头或指定绝对路径 url.
如果为了始终从 https://www.yourdomain.com/templates/item.json
获取数据,您应该将获取请求修改为 -> fetch("/templates/item.json")
或
指定绝对 url -> fetch("https://www.yourdomain.com/templates/item.json")
我正在使用 create-react-app
和 react-router-dom 4
制作 React 应用程序。我的问题是,当我加载默认“/”以外的 URL 时,相对路径会添加到正在发出的请求中。
例如,如果我在 url 搜索栏中输入 mysite.com/templates
,我的提取请求 fetch("templates/item.json")
请求 url: mysite.com/templates/templates/item.json
但如果我从使用来自 react-router-dom 的 Link
组件到 /templates
的主页不是这种情况。
有什么方法可以避免将相对路径添加到获取请求之前?
fetch
将加载相对于您打开的 url 的路径,如果您不以 /
开头或指定绝对路径 url.
如果为了始终从 https://www.yourdomain.com/templates/item.json
获取数据,您应该将获取请求修改为 -> fetch("/templates/item.json")
或
指定绝对 url -> fetch("https://www.yourdomain.com/templates/item.json")