我有這個(gè) axiosget方法可以從端點(diǎn)檢索用戶擁有的所有播放列表:getUserPlaylists() { axios .get("/v1/users/"+ JSON.parse(localStorage.getItem("user")).id +"/playlists", {headers: { Authorization: localStorage.getItem("apiKey") }}) .then((response) => { var playlists = []; for (var i = 0; i < response.data.items.length; i++) { playlists[i] = response.data.items[i]; } console.log(response); this.setState({ playlists: playlists, }); }) .catch((error) => console.log("Get Error")); }效果很好,我還編寫了一種方法來使用和 將post當(dāng)前視頻添加到播放列表videoIDplaylistIDaddToPlaylist(playlistID){ const uri = "/v1/users/"+ JSON.parse(localStorage.getItem("user")).id +"/playlists/"+ playlistID +"/videos" axios .post(uri, {id: window.location.href.substring(28) }, {headers: { Authorization: localStorage.getItem("apiKey") }}) .then((response) => { console.log(response); }) .catch((error) => console.log("Get Error")); }我嘗試在post方法中調(diào)用render方法如下:{this.state.playlists.map((playlist) => (<ul key={playlist.id}><Button type="button" className="btn btn-primary" onClick={this.addToPlaylist.bind(playlist.id)}>Add video to Playlist <b><i>{playlist.name}</i></b></Button></ul>))}一切正常,除了當(dāng)我單擊按鈕調(diào)用該post方法時(shí),我收到以下錯(cuò)誤POST http://localhost:3000/v1/users/867/playlists/[object%20Object]/videos 404 (Not Found)我不明白為什么該post方法無法正確獲取playlistID
在 React 中將變量傳遞給 axios 請(qǐng)求
茅侃侃
2022-10-21 15:25:16