我TM一直用vue的post提交数据,一直403,然后测试了N种方法,最后居然是被rest_rest_framework的认证系统 给坑惨了,我没定义认证,它却自动的给我默认上拒绝了 setting配置: REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": [], "DEFAULT_PERMISSION_CLASSES": [], } 完美解决
接口文件: from rest_framework.views import APIView from django.shortcuts import render,redirect,HttpResponse from api import models from django.http import JsonResponse class Register(APIView): def get(self,request): return render(request, "login.html") def post(self,request): username = str(request.data.get("register_username")) password = str(request.data.get("register_password")) message = {} try: models.UserInfo.objects.create(username=username,password=password) message[‘code‘] = 200 message[‘message‘] = "注册成功" return HttpResponse(message) except Exception as e: message[‘code‘] = 444 message[‘message‘] = "注册失败" return JsonResponse(message)
vue 文件: // 注册 register() { if (this.register_password != this.register_password_re) { alert("两次输入的密码不一致") } else if (this.register_username === "" || this.register_password === "" || this.register_password_re === "") { alert("输入框不能为空") } else { axios.post(‘/register/‘, { register_username: this.register_username, register_password: this.register_password }) .then(response => { if (response.data[‘code‘] ==200) { console.log(response.data) alert("注册成功") } else if (response.data[‘code‘] == 444) { alert("用户名已存在") } }).catch(error => { console.log(error) alert("请求异常") }) } },
原文地址:https://www.cnblogs.com/wanghong1994/p/12275784.html
时间: 2024-10-19 19:34:32