[英]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
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.