Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When 'localhost' '127.0.0.1' 'No 'Access-Control-Allow-Origin' header is present on the requested resource' #241

Closed
liuxue0905 opened this issue Jul 15, 2017 · 5 comments

Comments

@liuxue0905
Copy link

liuxue0905 commented Jul 15, 2017

I 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...

@adamchainz
Copy link
Owner

Because browsers send Origin: null for localhost/127.0.0.1, use http://127.0.0.1.xip.io/ instead and all will be fine.

@RenderCoder
Copy link

I have tried http://127.0.0.1.xip.io/.
It not work.

@Aliemeka
Copy link

I also tried http://127.0.0.1.xip.io/.
It not work

@adamchainz
Copy link
Owner

@Aliemeka and you're loading the page at 127.0.0.1.xip.io ?

@Aliemeka
Copy link

Aliemeka commented Jan 15, 2020 via email

Repository owner locked as off-topic and limited conversation to collaborators Jan 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants