跨源请求错误的解决方案

标签: 笔记

 方法一:拼数据(api为定义的装饰器,参考)

def api(func):
    @functools.wraps(func)
    def deal_with(*args, **kwargs):
        response_data = demjson.encode(func(*args, **kwargs))
        response = HttpResponse(response_data, content_type='application/json')
        response["Access-Control-Allow-Origin"] = "*"
        response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
        response["Access-Control-Max-Age"] = "1000"
        response["Access-Control-Allow-Headers"] = "*"
        return response
    return deal_with

主要部分为框里的五句 

 

 

 方法二:用别人写好的插件

安装:

pip install django-cors-headers 

##在settings.py中增加:

INSTALLED_APPS = (
  ...
  'corsheaders',
  ...
)

...

MIDDLEWARE_CLASSES = (
  ...
  'corsheaders.middleware.CorsMiddleware',
  'django.middleware.common.CommonMiddleware',
  ...
)
版权声明:本文为weixin_42336579原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42336579/article/details/81124265