reactjs - 我应该在哪里写 axios 代码来反应?
问题描述
axios
当我点击按钮时,我想有工作。然后,我写这样的代码。
axiosGet.js
showState = () => {
axios({
url: 'http://127.0.0.1:8000/profile/',
method: 'POST',
})
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
})
}
...
render() {
return (
...
<button onClick={this.showState}>Show state</button>
...
)
}
和views.py
class SettingView(viewsets.ModelViewSet) :
queryset = models.Category.objects.all()
serializer_class = CategorySerializer
def get(self, request) :
return Response("ok")
def post(self, request, format=None):
return Response("ok")
没有反应,没有错误。
我不应该在普通事件处理程序中使用 axios 吗?或者在反应中使用 axios 时我必须牢记什么要求?
解决方案
尝试这个
axios.get('http://127.0.0.1:8000/profile/')
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
在邮递员中检查你的 api 一次。
推荐阅读
- knime - 按加入日期绘制用户总数
- java - 使用 Websockets 和 JAVA 的 Watson Speech to Text 请求
- r - 排除 R 中 df 中顶部和底部 1% 的数据
- google-admin-sdk - 确定日历资源旧数据是否为会议室
- maven - 在 Maven 多模块包中创建 JAR 文件作为聚合
- sockets - PHP 警告:PHP 启动:无法加载动态库 '/usr/lib/php/extensions/no-debug-non-zts-20160303/gd2'
- c++ - 我的 OPENGL GLFW 窗口不随时间更新颜色
- hibernate - 当表没有主键时,@Entity 中的@Id 只能在一个事务中工作,而不是多个事务?
- sql - SQL:选择上次更改列值的行
- php - PHP,对象数组,我如何使用一组对象值作为数组键