Herkese Merhabalar,
Benim bir Django rest-api projesine sahibim. Front tarafında vue js kullandım.
Vue aracılığıyla istekte bulunurken aşağıdaki hatayı alıyorum.
Console çıktısı:
Access to XMLHttpRequest at 'https://api.iyziwell.com/api/user/login/' from origin 'https://main.d398abgajqt044.amplifyapp.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request
proje/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webpack_loader',
'account',
'rest_framework',
'rest_framework.authtoken',
'rest_framework_simplejwt',
'corsheaders',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddlew are',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMidd leware',
'django.contrib.messages.middleware.MessageMiddlew are',
'django.middleware.clickjacking.XFrameOptionsMiddl eware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
]
CORS_ORIGIN_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [ 'https://main.d398abgajqt044.amplifyapp.com', 'http://localhost:8082',
]
CORS_ALLOWED_ORIGIN_REGEXES = [ 'https://main.d398abgajqt044.amplifyapp.com', 'http://localhost:8082',
]
CORS_ALLOW_HEADERS = ( 'accept', 'accept-encoding', 'authorization', 'content-type', 'dnt', 'origin', 'user-agent', 'x-csrftoken', 'x-requested-with', )
proje/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='index.html'), name='index'),
path('api/client/',include('client.api.urls')),
path('api/user/',include('account.api.urls')),
path('api/appointment/',include('appointment.api.urls')),
path('api/user/login', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/user/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/password-reset/', include('django_rest_passwordreset.urls', namespace='password-reset')),
]
istek atan sayfa = https://main.d398abgajqt044.amplifyapp.com
istek alan sayfa = https://api.iyziwell.com/api/user/login/
Postman ile rahatça istek atabiliyorum lakin vue üzerinden istek atamıyorum.
Django Vue3 access-control-allow-origin is not allowed Hatası
8
●244
- 17-12-2022, 23:20:18
- 18-12-2022, 13:20:51aktan adlı üyeden alıntı: mesajı görüntüle
Hocam dediğinizi denedim lakin sonuç yine aynı. Nginxden de izinleri verdirttim. Hiçbir türlü istek kabul etmiyor - 18-12-2022, 15:32:29
from corsheaders.defaults import default_headers ALLOWED_HOSTS = ['*'] USE_X_FORWARDED_HOST = True CORS_ORIGIN_WHITELIST = ( "http://localhost:8082", "https://main.d398abgajqt044.amplifyapp.com", ) CSRF_TRUSTED_ORIGINS = CORS_ORIGIN_WHITELIST CORS_ALLOWED_ORIGINS = CORS_ORIGIN_WHITELIST CORS_ALLOW_HEADERS = list(default_headers) + [ "Özel-Header", "authorization" ] MIDDLEWARE = ( 'django.contrib.sessions.middleware.SessionMiddleware', # Bunun Altına corsheaders ekle 'corsheaders.middleware.CorsMiddleware', )Bu Configleri dener misin? - 18-12-2022, 17:54:12aktan adlı üyeden alıntı: mesajı görüntüle
Hocam denedim olmadı da bu sorunların Vue den kaynaklı olma ihtimali var mı?

arkadaş bu kodları yazmış
sunucuda ise https://enable-cors.org/server_nginx.html ayarları yaptırdık - 18-12-2022, 17:54:39DieAnotherDay adlı üyeden alıntı: mesajı görüntüle
Hayır chrome, opera hocam - 18-12-2022, 18:48:36Vue da Accept ve content type olması yeterli diger istekleri ngnix engelleyebilir. Çünkü allow origin axios veya fetch ile request edilirse ngnix güvenlik sebebiyle engeller. Ngnix aradan çıkarıp sunucuda ki vue ile kendi local Django ya istekte bulubursan çalışıp çalışmadığını anlarsın.
- 18-12-2022, 21:02:36aktan adlı üyeden alıntı: mesajı görüntüle
hocam localde test ettim "OPTIONS /api/user/login/ HTTP/1.1" 200 0 dönüyor yani bu yanlışım yok ise sorunun vue tarafından kaynaklı olduğu dimi ben şahsen biraz böyle yorumladım ama
