码迷,mamicode.com
首页 > 其他好文 > 详细

一 . Vue+Django 前后端数据交互知识点(一)

时间:2019-10-16 09:38:58      阅读:724      评论:0      收藏:0      [点我收藏+]

标签:alt   message   response   mon   请求   api   love   pre   cli   

一.  Vue+Django基础项目知识点

1. vue+django 跨域的问题解决

https://www.cnblogs.com/lovershowtime/p/11572569.html  跨域知识点

技术图片
在后台 解决跨域问题 使用中间件 作用全局

cors.py
class MiddlewareMixin(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        super(MiddlewareMixin, self).__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, process_request):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, process_response):
            response = self.process_response(request, response)
        return response

class CORSMiddleware(MiddlewareMixin):

    def process_response(self,request,response):
        # 添加响应头

        # 允许你的域名来获取我的数据
        response[Access-Control-Allow-Origin] = "*"

        # 允许你携带Content-Type请求头
        response[Access-Control-Allow-Headers] = "Content-Type"

        # 允许你发送DELETE,PUT
        response[Access-Control-Allow-Methods] = "DELETE,PUT"
        return response

MIDDLEWARE = [
    django.middleware.security.SecurityMiddleware,
    django.contrib.sessions.middleware.SessionMiddleware,
    django.middleware.common.CommonMiddleware,
    django.middleware.csrf.CsrfViewMiddleware,
    django.contrib.auth.middleware.AuthenticationMiddleware,
    django.contrib.messages.middleware.MessageMiddleware,
    django.middleware.clickjacking.XFrameOptionsMiddleware,
    api.cors.CORSMiddleware,
]

 

一 . Vue+Django 前后端数据交互知识点(一)

标签:alt   message   response   mon   请求   api   love   pre   cli   

原文地址:https://www.cnblogs.com/lovershowtime/p/11682196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!