簡體   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