繁体   English   中英

如何在 axios GET 请求中使用我的浏览器 url 上的参数

[英]How do I use the params on my browser url on axios GET request

目前,我已经能够使用

let {id} = useParams();

当我打电话

{id} 在我的组件中,我从浏览器的 url 获取 id。 但是我需要它来让我从我的数据库中获取数据

我试过了

const getData = () => {
     axios.get(`/API/joindiscussion/` + {id}, {
     //do something
     }):
}

但它没有用

nodejs api路由/API/joindiscussion/:id

传入 react <Router path="/exmple/:id" element={} />

示例组件使用 const id =useParams()

const getData = () => {

 axios.get(`/API/joindiscussion/${id}`, {
 //do something

 }):

}

要么

nodejs api路由/API/joindiscussion?id=123

const getData = () => {

 axios.get(`/API/joindiscussion?${id}`, {
 //do something
 }):

}

ES6 javascript 中的上述概念

检查您的 API 以及它如何获取 id(我假设您对参数和查询方法感到困惑)。

如果 API 采用参数格式的 id,(例如: /API/joindiscussion/1234 ,其中 1234 是 id),那么您的代码应该是

axios.get(`/API/joindiscussion/` + id,

如果 API 采用查询格式的 id(例如: /API/joindiscussion/?id=1234 ,其中 1234 是 id),那么您的代码应该是

axios.get(`/API/joindiscussion/?id=` + id,

如果您有任何疑问,请发表评论。

像这样:

let param = useParams()
  axios.get(`url/${param.id}`).then(response => {
    // do something with the response
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM