Solving Cross-Origin Request Issues in Django
Several methods Use django-cors-headers for global control**(recommended)** Use JsonP, only for Get method Set response headers in views.py, can only control a single endpoint django-cors-headers First install pip install django-cors-headers Then just configure it in settings.py INSTALLED_APPS = [ ... 'corshe
目录4 个章节
Several Methods
- Use
django-cors-headersfor global control (recommended) - Use
JsonP, only works with the Get method - Set response headers in
views.py, can only control a single endpoint
django-cors-headers
First, install
pip install django-cors-headers
Then just configure it in settings.py
INSTALLED_APPS = [
...
'corsheaders',
...
]
MIDDLEWARE_CLASSES = (
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware', # 注意顺序
...
)
#跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'*'
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
That's it.
JsonP
When using Ajax to fetch json data, there are cross-origin restrictions. However, calling js script files on a web page is not affected by cross-origin restrictions, and JSONP takes advantage of this to achieve cross-origin transfer. Therefore, we need to change the dataType in the Ajax call from JSON to JSONP (the corresponding API also needs to support JSONP) format. JSONP can only be used for GET requests. This article will not cover this method; those who need it please search on your own
Configuring Response Headers in Views.py
Modify the implementation function of the corresponding API in views.py to allow other domains to request data via Ajax:
def myview(request):
response = HttpResponse(json.dumps({“key”: “value”, “key2”: “value”}))
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
_EOF_
Solving Cross-Origin Request Issues in Django
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Django清空所有数据和重建表结构
清空所有数据 python manage.py flush 重建表结构 删除数据库中所有表 删除应用文件夹下的migrations目录内所有文件,除了__init__.py 执行以下命令 python manage.py makemigrations python manage.py migrate
2021-12-18 · 0_Django基础知识 · 约 1 分钟
Django使用django-filer上传图片
前言 (好像不知道写啥) 注意 django-filer目前只支持到django 1.11 setting.py INSTALLED_APPS = [ # image support 'easy_thumbnails', 'filer', 'mptt', ] # Image Upload Support THUMBNAIL_HIGH_RESOLUTION = Tr
2022-05-27 · 0_Django基础知识 · 约 1 分钟
Django应用中的静态文件处理
在日常开发中,我们都是把Django的Debug模式打开,方便调试,在这个模式下,由Django内置的Web服务器提供静态文件服务,不过需要进行一些配置,才能正确访问。 配置settings # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ ST
2021-12-18 · 0_Django基础知识 · 约 1 分钟
Maven国内镜像配置
配置maven安装目录下的conf/settings.xml。 在<mirrors>节点下,设置: mirror idaliyun-public/id mirrorOf*/mirrorOf namealiyun public/name urlhttps://maven.aliyun.com/repository/public/url /mir
2021-12-18 · Java · 约 1 分钟
讨论
共 0 条加载评论中…