Django Python rest框架,没有'访问控制 - 允许 - 来源'标题出现在请求的资源在铬,在Firefox中的作品 [英] Django Python rest framework, No 'Access-Control-Allow-Origin' header is present on the requested resource in chrome, works in firefox

查看:967
本文介绍了Django Python rest框架,没有'访问控制 - 允许 - 来源'标题出现在请求的资源在铬,在Firefox中的作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在相同的问题上研究和阅读了很多Stackoverflow帖子。没有人解决了我的问题。



我的问题是,我得到...没有'Access-Control-Allow-Origin'标题存在于请求的资源中...错误我的控制台。



我正在使用: b
$ b

Chrome版本57.0.2987.133
Firefox版本52.0。 2



Python 2.7
Django 1.11a1


AngularJS



我使用MAMP来为我的前端Angular服务,并为后端服务提供django服务器。

在我的django设置中, cors中间件,并尝试使用白名单方法,并将所有设置为true:

  MIDDLEWARE = [

'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common。 CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.A uthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',

]

CORS_ORIGIN_ALLOW_ALL = True

在google chrome上,我仍然收到这样的错误:


localhost /:1 XMLHttpRequest无法加载{my endpoint url}。通过CORS策略将
从{my endpoint url}重定向到{my endpoint url}已被
阻止:No
上存在'Access-Control-Allow-Origin'标头资源。 Origin {requests url}因此不是
允许访问的。


它适用于Firefox,我无法计算为什么它不适用于谷歌浏览器。我还没有尝试过任何其他类型的浏览器。

解决方案

使用

安装cors-headers包

  pip install django-cors-headers 

添加到您安装的应用程序中

  INSTALLED_APPS =(

...
' corsheaders',
...

添加MIDDLEWARE
记得添加为列表中的第一个

  MIDDLEWARE = [

'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]

在安装应用程序之前,这个配置可以让任何人访问

  CORS_ORIGIN_ALLOW_ALL = True 

或创建点击列表

  CORS_ORIGIN_WHITELIST =(

'google.com',
'hostname.e xample.com',
'localhost:8000',
'127.0.0.1:9000'


I have researched and read quite a few Stackoverflow posts on the same issue. None have resolved my issue.

My problem is that I am getting the "...No 'Access-Control-Allow-Origin' header is present on the requested resource..." error in my console.

I am using:

Chrome Version 57.0.2987.133 Firefox Version 52.0.2

Python 2.7 Django 1.11a1

AngularJS

I am using MAMP to serve my front-end Angular stuff, and the django server for the backend stuff.

In my django settings I have included the cors middleware and tried both the whitelist approach and just setting all to true:

MIDDLEWARE = [

    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

]

CORS_ORIGIN_ALLOW_ALL = True

On google chrome I still get this error:

localhost/:1 XMLHttpRequest cannot load {my endpoint url}. Redirect from {my endpoint url} to {my endpoint url with a } has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin {requesting url} is therefore not allowed access.

It works appropriately on Firefox, and I can't figure out why it won't work for google chrome. I haven't tried any other types of browsers. Any help will be very appreciated, thank you.

解决方案

Install the cors-headers package with

pip install django-cors-headers

Adds to your installed apps

INSTALLED_APPS = (

    ...
    'corsheaders',
    ...
)

Add on your MIDDLEWARE remember to add as being the first in the list

MIDDLEWARE = [  

    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

Before installed apps put this configuration for anyone to access

CORS_ORIGIN_ALLOW_ALL=True

Or create a list of hits

CORS_ORIGIN_WHITELIST = (

    'google.com',
    'hostname.example.com',
    'localhost:8000',
    '127.0.0.1:9000'
)

这篇关于Django Python rest框架,没有'访问控制 - 允许 - 来源'标题出现在请求的资源在铬,在Firefox中的作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆