简体   繁体   English

我在React中编写axios post方法并添加Access-Control-Allow-Origin,但请求无法正常工作,并且响应我cors原点错误

[英]I write axios post method in React and add Access-Control-Allow-Origin but request is not working and response to me cors origin error

I write axios post method in React and add Access-Control-Allow-Origin but the request is not working and send this response: 我在React中编写axios post方法并添加Access-Control-Allow-Origin,但是请求无法正常工作并发送以下响应:

Access to XMLHttpRequest at ' https://api.aaaa.com/aaa/login/ ' from origin ' http://0.0.0.0:3000 ' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response. CORS策略已阻止从原点' http://0.0.0.0:3000 '访问' https://api.aaaa.com/aaa/login/ '上的XMLHttpRequest:请求标头字段access-control-allow-origin飞行前响应中,Access-Control-Allow-Header不允许使用。

axios.post('https://api.aaa.com/aaa/login/', userlogin , {
            headers :{
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': 'http://api.aaa.com',
                'Access-Control-Allow-Credentials': true,
            }
        })

In Backend Code (Django), I add corsheaders Package and CORS_ALLOW_CREDENTIALS = True 在后端代码(Django)中,我添加了corsheaders软件包,并且CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [' http://0.0.0.0:3000/ '] CORS_ORIGIN_WHITELIST = [' http://0.0.0.0:3000/ ']

How can I fix this error? 如何解决此错误?

CROS has to be added from webservice , for example if the api( https://api.aaaa.com/aaa/login/ ) is written in php then in php script add below headers : 必须从Web服务中添加CROS,例如,如果api( https://api.aaaa.com/aaa/login/ )是用php编写的,则在php脚本中添加以下标头:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");

For Django: 对于Django:

pip install django-cors-headers

and then add it to your installed apps: 然后将其添加到已安装的应用程序中:

INSTALLED_APPS = (
    ...
    'corsheaders',
    ...
)

You will also need to add a middleware class to listen in on responses: 您还需要添加一个中间件类来侦听响应:

MIDDLEWARE_CLASSES = (
    ...
    'corsheaders.middleware.CorsMiddleware',  
    'django.middleware.common.CommonMiddleware',  
    ...
)

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
    'localhost:3030',
)
CORS_ORIGIN_REGEX_WHITELIST = (
    'localhost:3030',
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Axios blocked by CORS policy with Django REST Framework: 'Access-Control-Allow-Origin' header in the response must not be wildcard - Axios blocked by CORS policy with Django REST Framework: 'Access-Control-Allow-Origin' header in the response must not be wildcard AngularJS HTTP POST无访问控制-允许起源CORS - AngularJS HTTP POST No Access-Control-Allow-Origin CORS “请求 header 字段 Access-Control-Allow-Origin 在预检响应中被 Access-Control-Allow-Headers 不允许”尽管 CORS 配置有效 - “Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response” despite valid CORS config Django CORS Access-Control-Allow-Origin缺失 - Django CORS Access-Control-Allow-Origin missing Flask CORS - 重定向上没有Access-control-allow-origin标头() - Flask CORS - no Access-control-allow-origin header present on a redirect() 被 CORS 策略阻止:No"Access-Control-Allow-Origin" Using Flask - Blocked by CORS policy: No"Access-Control-Allow-Origin" Using Flask Django CORS 问题:不允许访问控制允许来源 - Django CORS issue: access-control-allow-origin is not allowed 由于没有 Access-Control-Allow-Origin 标头但标头存在而被 CORS 阻止的 HTTP 请求 - HTTP request blocked by CORS for not having Access-Control-Allow-Origin header but the header is present 指向托管 Django 服务器的本地 React 应用程序 CORS 没有“访问控制允许来源”header - Local React app pointing to hosted Django server CORS no 'Access-Control-Allow-Origin' header django-cors-headers 不工作:请求的资源上不存在“Access-Control-Allow-Origin”header - django-cors-headers not working: No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM