首页 > 解决方案 > 我应该在哪里写 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 时我必须牢记什么要求?

标签: reactjspostgetdjango-rest-frameworkaxios

解决方案


尝试这个

axios.get('http://127.0.0.1:8000/profile/')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
        console.log(err);
    });

在邮递员中检查你的 api 一次。


推荐阅读