跨域请求被阻止-使用Axios和Django REST [英] Cross-Origin Request Blocked - Using Axios and Django REST

查看:66
本文介绍了跨域请求被阻止-使用Axios和Django REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在节点服务器上使用本地服务(端口8080)的应用程序中发出GET请求.我正在使用Axios向同时也在本地(端口8000)提供服务的django REST服务器发出请求.

I am trying to make a GET request in my application that is being served locally (port 8080) using on a node server. I am using Axios to make the request to a django REST server that is also being served locally (port 8000).

我的请求如下:

axios.get('http://127.0.0.1:8000/recipes/',{headers: {"Access-Control-Allow-Origin": "*"}})

在Django方面,我已将它们包含在我的中间件中

On the Django side, I've included these in my middleware

MIDDLEWARE = [
    'django.middleware.common.CommonMiddleware',
    'corsheaders.middleware.CorsMiddleware',
]

,并且在我已安装的应用中:

and this in my installed apps:

INSTALLED_APPS = [
    'corsheaders',
]

并包含此设置:

CORS_ORIGIN_ALLOW_ALL = True

但是我仍然收到CORS错误:

But I'm still getting a CORS error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:8000/recipes/. (Reason: missing token ‘access-control-allow-origin’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel).

知道我缺少什么吗?

推荐答案

我知道了.

我需要删除:

{headers: {"Access-Control-Allow-Origin": "*"}

从请求中输入

.显然,标头应仅是响应的一部分.

from the request. Apparently that header should only be part of the response.

删除后,一切正常.

这篇关于跨域请求被阻止-使用Axios和Django REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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