reactjs - 使用 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)
}
解决方案
推荐阅读
- html - 使用纯 CSS 悬停时的 SVG *pattern* 笔触颜色变化?
- java - 使用 spring r2dbc 时找不到 javax.persistence 注释
- laravel - 在 Laravel 上进行支付时使用未定义的常量 INTL_IDNA_VARIANT_UTS46 错误
- javascript - 以干净的方式将 URL 拆分为数组的最佳方法?
- javascript - 我可以将 php 注入我的 javascript 文件以注入 php 文档吗?
- python-3.x - 如何在 Keras 中使用 maxPooling 和卷积层修复 ValueError?
- php - php计算器数量乘以指定选项
- javascript - 根据表单输入更改 Datepicker 格式和输出
- sql - 创建一个过程来合并数据并避免重复
- c# - 使用任何 cpu 编译时出现 System.BadImageFormatException