Vue.js {detail:“未提供身份验证凭据”。}使用simplejwt获取请求时

Vue.js {detail:“未提供身份验证凭据”。}使用simplejwt获取请求时,vue.js,django-rest-framework,axios,Vue.js,Django Rest Framework,Axios,我看到了很多类似的问题,但这些问题对我来说并不适用。我真的不知道为什么我不能访问api。我用邮递员的令牌测试了它,它返回了正确的数据。请你纠正我好吗 后端 # model class Department(models.Model): name = models.CharField(max_length=100 # viewset class DepartmentViewSet(viewsets.ModelViewSet): queryset = models.Departme

我看到了很多类似的问题,但这些问题对我来说并不适用。我真的不知道为什么我不能访问api。我用邮递员的令牌测试了它,它返回了正确的数据。请你纠正我好吗

后端

# model
class Department(models.Model):
    name = models.CharField(max_length=100

# viewset
class DepartmentViewSet(viewsets.ModelViewSet):
    queryset = models.Department.objects.all()
    serializer_class = serializers.DepartmentSerializer
    permission_classes = (IsAdminUser,)

#setting.py
CORS_ALLOWED_ORIGINS = [
    "http://localhost:8080",
    "http://127.0.0.1:8080",
]
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
}
前端

# axios-base.js
import axios from "axios";

const getAPI = axios.create({
    baseURL: 'http://127.0.0.1:8000',
    timeout: 1000,
})

export {getAPI}

#template.vue
<script>
import { getAPI } from "../../src/axios-base.js";
export default {
  name: "Department",
  data() {
    return {
      departments: [],
    };
  },
  mounted() {
    this.getAllDepartment();
  },
  methods: {
    getAllDepartment: function() {
      const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl9....."; <- access token
      getAPI.get("/attendance-rest-api/departments/",
          {
              email: "admin@gmail.com", <- admin account
              password: "admin",
          },
          {
            headers: {
              Authorization: `Bearer ${token}`,
              "Content-Type": "application/json",
            },
          }
        )
        .then((res) => {
          this.departments = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
我在django终点站出错了

Unauthorized: /attendance-rest-api/departments/
[20/Sep/2020 13:18:04] "GET /attendance-rest-api/departments/ HTTP/1.1" 401 58
浏览器中出现错误

detail: "Authentication credentials were not provided."

你误用了Axios API。在GET请求中,只应将一个参数--
config
object传递到相应的方法中,并且应在该对象的
params
属性中传递params。像这样:

getAPI.get("/attendance-rest-api/departments/",
  {
    params: {
      email: "admin@gmail.com", <- admin account
      password: "admin",
    },
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json",
    },
  }
)
getAPI.get(“/attention rest api/departments/”,
{
参数:{

电子邮件:“admin@gmail.com“,感谢您的回复。我正在学习使用django rest framework和axios,这就是为什么我对它有点困惑的原因。这就是全部目的。我想从部门模型中获取数据,并将其显示在注册表表单中,以便用户选择他们所在的部门(许多关系)在
Department
的视图集中,我将权限设置为
isadmin用户
,因此我认为我必须在get请求中传递admin帐户才能访问api。但我将尝试阅读更多关于axios和django rest framework的信息。如果您希望在注册表中显示某些内容,这基本上是公开信息(因为你在用户注册之前对他们一无所知)。你是对的!我想我对此有一些误解。谢谢你,先生!!
getAPI.get("/attendance-rest-api/departments/",
  {
    params: {
      email: "admin@gmail.com", <- admin account
      password: "admin",
    },
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json",
    },
  }
)