gpt4 book ai didi

reactjs - 添加到获取请求的相对路径

转载 作者:行者123 更新时间:2023-12-02 20:16:34 25 4
gpt4 key购买 nike

我正在使用 create-react-appreact-router-dom 4 制作一个 React 应用程序。我的问题是,当我加载默认“/”以外的 URL 时,相对路径会添加到正在发出的请求之前。

例如,如果我在 url 搜索栏中输入 mysite.com/templates,我的提取请求 fetch("templates/item.json") 请求 url:mysite.com/templates/templates/item.json 但如果我使用来自 react-router 的 Link 组件从主页导航到 /templates -dom 情况并非如此。

有什么方法可以避免相对路径被添加到获取请求之前?

最佳答案

如果您不以 / 开头或指定绝对 url,

fetch 将加载相对于您打开的 url 的路径。

如果要始终从 https://www.yourdomain.com/templates/item.json 获取数据,您应该将获取请求修改为 -> fetch("/templates/item.json")

指定绝对 url -> fetch("https://www.yourdomain.com/templates/item.json")

关于reactjs - 添加到获取请求的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52260498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com