我在App.js文件中使用如下反应路由器:
<Route exact path="/category/:categoryId">
<ProductLists />
</Route>
并被称为:
<RouterLink to={`/category/${category.id}`}>
{category.name}
</RouterLink>
但问题是,当我试图在ProductLists组件中发出HTTP请求时;类别";在请求的开头。
这是提出请求的代码:
Axios.get(
`/api/resources/products/category-id/?categoryId=${categoryId}`,
{
headers: {
'Content-Type': CONTENT_TYPE_JSON_VALUE,
},
}
因此,与其这样做:
http://localhost:3000/api/resources/products/category-id/?categoryId=10a49ef4
它正在这样做:
http://localhost:3000/category/api/resources/products/category-id/?categoryId=10a49ef4
知道为什么";类别";是否插入在请求的开头?
谢谢!
感谢@Yousaf,我找到了答案。
我只需要像这个一样添加baseURL:"/">
Axios.get(
`/api/resources/products/category-id/?categoryId=${categoryId}`,
{
baseURL: '/',
headers: {
'Content-Type': CONTENT_TYPE_JSON_VALUE,
},
}
希望这将帮助未来的