我正在傳遞一個物件陣列(硬編碼值)以使用 axios 發出 get 請求,但每次都會出現這樣的錯誤:

在這方面需要幫助,我是反應 js 的新手。在我看來,我在制作物件陣列時犯了錯誤,或者我認為我們不能將 const 傳遞給 axios?
函式和物件陣列(硬編碼值)
const api =
[
{
"description": "",
"durationday": "fullday",
"end": "null",
"id": 0,
"location": "",
"start": "null",
"title": "",
},
{
"description": "yaaa!",
"durationday": "absent",
"end": "10-04-2021 12:10:00",
"id": 1,
"location": "perth",
"start": "10-04-2021 12:10:00",
"title": "holiday",
},
{
"description": "busy",
"durationday": "fullday",
"end": "10-22-2021 12:10:00",
"id": 2,
"location": "melbourne",
"start": '10-19-2021 12:00:00',
"title": "working",
},
{
"description": "dd",
"durationday": "halfday",
"end": "10-24-2021 12:10:00",
"id": 3,
"location": "updated perth",
"start": "10-24-2021 12:10:00",
"title": "updated holiday",
}
]
console.log(api)
const getEvents = () => {
axios.get(api)
.then(response => {
console.log(response.data)
})
.catch(err => console.log("This is error for fetching", err))
}
useEffect(() => {
getEvents()
}, []);
uj5u.com熱心網友回復:
這里有幾件事可以發出 API REST 請求:
1- 它是一個 GET 請求,要遵循 API REST 規則,您應該使用 GET 來檢索資源,因此您不應傳遞正文。
2- 發出 api 請求時必須使用的第一個引數是作為字串的 url 端點,例如:axios.get(“/endpointPath”).then...。
3- 要將所有資料發送到服務器,您可以將其作為 POST 請求提交新資料。例如: axios.post(“/endpointPath”,body).then... 在這種情況下,主體可能是您的 api const(如果您不使用郵遞員或其他客戶端,則無需將其作為 json 傳遞)
4- 您可能會發現這很有用:https : //stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/318432.html
標籤:javascript 反应 json 接口 公理
