2021年5月1日星期六

CORS not working in Django but settings seem correct

I am trying to make a POST call to Django from a React Native Web front end on different subdomains.

I thought I had configured CORS correctly, but that does not seem to be the case.

Here's what my Django settings.py looks like:

CORS_ALLOW_CREDENTIALS = True    CORS_ALLOW_HEADERS = ['*']    CORS_ALLOWED_ORIGINS = ['https://api.example.com', 'https://example.com', 'https://www.example.com' ]    CSRF_TRUSTED_ORIGINS = [      'https://api.example.com', 'https://example.com', 'https://www.example.com'  ]    ALLOWED_HOSTS = ["0.0.0.0", "api.example.com", "example.com"]    MIDDLEWARE = [      'corsheaders.middleware.CorsMiddleware',      'django.middleware.common.CommonMiddleware',      'django.contrib.sessions.middleware.SessionMiddleware',      'django.contrib.auth.middleware.AuthenticationMiddleware',      'django.contrib.messages.middleware.MessageMiddleware',      'django.middleware.clickjacking.XFrameOptionsMiddleware',      'django.middleware.security.SecurityMiddleware',      'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',  ]    INSTALLED_APPS = [       ...      'corsheaders',       ...  ]  

What exactly am I doing wrong here? The error I'm getting is this:

Access to XMLHttpRequest at 'https://api.example.com/api/v1/pagescreate/' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.  

And this is my Django view:

class PageCreateView(generics.CreateAPIView):      queryset = Page.objects.all()      serializer_class = PageSerializer  

What could be causing this? Am I missing some setting in React? I'm using axios to make the calls, with the only header being "Content-Type": "application/json"

EDIT: Could this be due to some nginx rule on my server? Or maybe my Kubernetes configuration? I am using Docker to set it up the container and can easily link the Dockerfile, or any information from my Kubernetes setup

https://stackoverflow.com/questions/67327660/cors-not-working-in-django-but-settings-seem-correct April 30, 2021 at 11:01AM

没有评论:

发表评论