分离的前后台交互
后台处理跨域
安装插件
>: pip install django-cors-headers
插件参考地址:https://github.com/ottoyiu/django-cors-headers/
项目配置:dev.py
# 注册app
INSTALLED_APPS = [
...
'corsheaders'
]
# 添加中间件
MIDDLEWARE = [
...
'corsheaders.middleware.CorsMiddleware'
]
# 允许跨域源
CORS_ORIGIN_ALLOW_ALL = True
跨域测试
后端
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
class Test(APIView):
def get(self, request,*args,**kwargs):
return Response('ok')
前端
export default {
name: 'home',
components: {
Header,
Banner,
},
created() {
this.$axios({
url: 'http://127.0.0.1:8000/user/text/',
}).then(response => {
console.log(response)
})
}
}