When 'localhost' '127.0.0.1' 'No 'Access-Control-Allow-Origin' header is present on the requested resource'
See original GitHub issueI used django-cors-headers, and config it.
Backend: Django + django-rest-framework
settings.py
ALLOWED_HOSTS = ['*']
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CORS_ORIGIN_ALLOW_ALL = True
python manage.py runserver 0.0.0.0:8000
Frontend: vue + axios I try 3 url to access http://ip:8000/api/xxx http://127.0.0.1:8000/api/xxx http://localhost:8000/api/xxx
When request host is ip(192.168.1.xxx): It worked ok.
When request host is ‘localhost’ or ‘127.0.0.1’: It did not work
XMLHttpRequest cannot load http://127.0.0.1:8000/api/cameras.
Redirect from 'http://127.0.0.1:8000/api/cameras' to 'http://127.0.0.1:8000/api/cameras/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8080' is therefore not allowed access.
Question:
Why ‘127.0.0.1’ & ‘localhost’ can not work well, I added CORS_ORIGIN_ALLOW_ALL = True
already…
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
No 'Access-Control-Allow-Origin' header is present on the ...
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Read more >Resolve the "No 'Access-Control-Allow-Origin' header" error ...
In the new policy, under Cross-origin resource sharing, turn on CORS. Fill in other settings as needed and choose Create policy.
Read more >Reason: CORS header 'Access-Control-Allow-Origin' missing
The response to the CORS request is missing the required Access-Control-Allow-Origin header, which is used to determine whether or not the ...
Read more >Access-Control-Allow-Origin header missing
No these are just http requests, am developing locally. I tried putting an explicit origin as well but that didn't work either.
Read more >Response to preflight request doesn't pass access control check
... check: No 'Access-Control-Allow-Origin' header is present on the requested resource. ... 'localhost, 127.0.0.1' // 'localhost,localhost:8080,127.0.0.1 ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Because browsers send
Origin: null
for localhost/127.0.0.1, usehttp://127.0.0.1.xip.io/
instead and all will be fine.I have tried
http://127.0.0.1.xip.io/
. It not work.