微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django AJAX方式提交数据解决csrf验证问题

    <script type="text/javascript">
        axios.defaults.baseURL = 'http://xx.xx.xx.xx';
        axios.defaults.headers.common['X-CSrftoken'] = '{{ csrf_token }}';
        Vue.prototype.$http = axios;
        var app = new Vue({
            el: '#app',
            data: {
                res: [],
                form: {
                    sday: '2020-05-27',
                    eday: '2020-05-27',
                }
            },
            methods: {
                loadData() {
                    this.$http.post('/tongji_plate_entry/', app.form)
                        .then(function (response) {
                            if (response.data.result == 'SUCCESS') {
                                app.res = response.data.res;
                                app.form.sday = response.data.sday;
                                app.form.eday = response.data.eday;
                            }
                        })
                        .catch(function (error) {
                            alert(error);
                        });
                }
            }

        });
    </script>

就是设置axios的headers

axios.defaults.headers.common['X-CSrftoken'] = '{{ csrf_token }}';

文章分享完毕,希望我的分享对大家有所帮助。更多学习技巧也可参阅:网站源码、模板、教程

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐