首页 > 解决方案 > 使用 Axios 获取 API 数据并将其显示在带有钩子的 React 应用程序中

问题描述

我知道反应,但我在 django 中很新。尝试使用 axios 与 django 连接反应。我想将我的对象打印到控制台。Django 服务器在 localhost:8000 中运行,我不知道我是否在 axios GET 请求中正确设置了我的 url?(我的模型名称在 django 中是“artifact”)关于这个问题你可以提供更多的资源吗?

 const [artifacts, getArtifacts] = useState(''); 

 const url = 'http://localhost:8000/'; 

 useEffect(() => {
  getAllArtifacts(); 
 }, []); 


  const getAllArtifacts = () => {
   axios.get(`${url}`)
  .then((response) => {
     const allArtifacts = response.data.artifacts.allArtifacts; 
    getArtifacts(allArtifacts); 
  })
.catch(error => console.error(`Error: ${error}`)); 
 }

 const [percentage, setPercentage] = useState(0)
 const onChange = (e) => {
  setPercentage(e.target.value)
  }

标签: reactjsapidjango-rest-frameworkaxios

解决方案


推荐阅读