Axios受到使用Django REST Framework的CORS策略的阻止

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Axios受到使用Django REST Framework的CORS策略的阻止相关的知识,希望对你有一定的参考价值。

我正在尝试使用Axios向我的API(Django REST Framework)发出请求,但是我收到以下错误:

Access to XMLHttpRequest at 'http://trvl.hopto.org:8000/api/airports/MSP/routes' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

即使您使用cUrl检查:

curl -I -X GET   -H "Origin: http://localhost:3000"   -H 'Access-Control-Request-Method: GET'   http://trvl.hopto.org:8000/api/airports/MSP/routes 2>&1 | grep 'Access-Control-Allow-Origin'
Response: Access-Control-Allow-Origin: *

Full response from cUrl Options:
OPTIONS request: curl -I -X OPTIONS   -H "Origin: http://localhost:3000"   -H 'Access-Control-Request-Method: GET'   http://trvl.hopto.org:8000/api/airports/MSP/routes
HTTP/1.1 200 OK
Date: Wed, 27 Mar 2019 10:58:01 GMT
Server: WSGIServer/0.2 CPython/3.6.8
Content-Type: text/html; charset=utf-8
Content-Length: 0
Vary: Origin
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: accept, accept-encoding, authorization, content-type, dnt, origin, user-agent, x-csrftoken, x-requested-with
Access-Control-Allow-Methods: DELETE, GET, OPTIONS, PATCH, POST, PUT
Access-Control-Max-Age: 86400

使用Axios时:

let url = 'http://A.hopto.org:8000/api/airports/MSP/routes';

axios.get(url)
      .catch((err) => {
        console.error(err);
      })
      .then((response, ) => {
        console.log(response);
      });


Response:
Access to XMLHttpRequest at 'http://API.hopto.org:8000/api/airports/MSP/routes' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

问题是API启用了CORS但我无法在我的WebApp中使用Axios和React。

更新:

Here is my Django settings.py I'm using the https://github.com/ottoyiu/django-cors-headers module.

**settings.py**

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'trvl',
    'rest_framework',
    'coreapi',
    'django_filters',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'corsheaders.middleware.CorsPostCsrfMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware'
]

CORS_ORIGIN_ALLOW_ALL = True
答案

在你的后端代码上你需要添加添加http://localhost:3000作为字段Access-Control-Allow-Origin的响应头的一部分

还要确保你的回复有这个字段Access-Control-Allow-Methods包括GET

您提到您的后端API代码已启用CORS。如果上述方法无效,您是否可以更新帖子以包含如何设置?

以上是关于Axios受到使用Django REST Framework的CORS策略的阻止的主要内容,如果未能解决你的问题,请参考以下文章

使用 Axios 从前端访问 Django Rest API 时出现 Cors 错误

Axios 被 Django REST 框架的 CORS 策略阻止

如何使用 Axios 将 CSRF Coo​​kie 从 React 发送到 Django Rest Framework

记录对 django-rest-framework 的请求

django rest + axios put请求错误403

无法从 reactjs 中的 axios 向 id=1 文章的 django REST API 后端发出 GET 请求