#先导入django-cors-headers
pip install django-cors-headers
#在项目的setting.py中加入
INSTALLED_APPS=[
....
corsheaders
]
# 需要将添加项放在最上面,防止出现访问不了的问题
MIDDLEWARE=[
'corsheaders.middleware.CorsMiddleware'
...
]
# 添加完上面2项后 新增:
# 表示允许跨域访问的网址,单独加网址并不能完全解决问题。
CORS_ALLOWED_ORIGINS = [
'http://localhost:9527'
]
# 需要增加:
CORS_ALLOW_HEADERS = (
'accept'
'authorization'
'content-type'
'user-agent'
'x-csrftoken'
'x-requested-with'
'x-token'
)
#错误实例:
#错误内容为:from origin 'http://localhost:9527' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Access-Control-Allow-Origin:表示需要再CORS_ALLOED_ORIGINS中增加网址。
看报错内容,会将网址显示出来,上图为'http://localhost:9527'
Django处理CROS问题
最新推荐文章于 2024-04-27 18:55:57 发布