django rest + axios put request error 403

时间:2018-02-27 03:38:33

标签: django rest django-rest-framework axios webpack-dev-server

我已阅读重复内容,似乎没有任何效果。我可以直接从url中的表单执行put请求,但我似乎无法让axios请求正常工作。

我试过了:

CSRF with Django, React+Redux using Axios https://gist.github.com/paltman/490049a64fa4115a2cea

我的view.py:

class FrequencyList(generics.ListCreateAPIView):
    queryset = Frequency.objects.all()
    serializer_class = FrequencySerializer


class FrequencyDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Frequency.objects.all()
    serializer_class = FrequencySerializer

我的axios请求:

axios({
        method: 'put',
        url: '/f/'+id,
        data: {
            item: item,
        },
    }).then(function (response) {
        this.setState({needReceipt: true});
    })
    .catch(function (error) {
        console.log(error);
    });

在我的settings.py中:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
}

在我的webpack.config.dev.js中:

const axios = require('axios');

axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.xsrfCookieName = "csrftoken";

1 个答案:

答案 0 :(得分:0)

试试这个

axios.put('/f/'+id, { item: item })
    .then(function(response){
        this.setState({needReceipt: true});
});

Reference