使用React JS的Axios数据参数问题[已关闭]

问题描述 投票:0回答:0

我一直尝试使用 Axios 发送 API 请求,看起来从数据参数读取用户 id 时出现问题,我正在从本地存储读取用户 id

const getAuthUser=(data)=>{
    if(localStorage.getItem("user")){
        return JSON.parse(localStorage.getItem("user"))
    }
}

 const localStorage = require("../helper/Local_storage") 
 const auth= localStorage.getAuthUser() 

 useEffect(()=>{
    let userId = JSON.stringify({
      "user_id": auth.id
    });
    
    let config = {
      method: 'get',
      maxBodyLength: Infinity,
      url: 'http://localhost:4000/borrowing_request',
      headers: { 
        'token': auth.token, 
      },
      data: userId
    }; 
    axios.request(config)
    .then((res) => {
      setBorrowedBooks({...borrowedBooks, results:res.data, loading:false})
    })
    .catch((err) => {
      console.log(err);
    });
  },[])

但是,当使用 Postman 测试 API 时,它可以工作 Postman screenshot

reactjs http axios local-storage get-request
© www.soinside.com 2019 - 2024. All rights reserved.