Skip to content

Cors Error #60

Open
Open
@ikrma47

Description

@ikrma47

Hello,

I've been stuck at cors issue with reconnecting event source.
All my others API's are working fine but when I use ReconnectingEventSource I start facing Cors error.

my code at frontend with react

function App() {

  useEffect(() => {


    let es = new ReconnectingEventSource("http://localhost:8000/events/")
    es.onopen = function (){
      console.log("connected")
    }
  },[])
}

backend code

settings.py

CORS_ORIGIN_ALLOW_ALL = True
ALLOWED_HOSTS = ["localhost"]

CORS_ALLOWED_ORIGINS = ["http://localhost"]
CORS_ALLOW_CREDENTIALS = False


INSTALLED_APPS = [
    ....
    'channels',
    'django_eventstream',
    'corsheaders',
   ....
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    ....
]

asgi.py

import os

from django.core.asgi import get_asgi_application
from django.conf.urls import url
from channels.routing import ProtocolTypeRouter, URLRouter
# from channels.auth import AuthMiddlewareStack
import django_eventstream

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

application = ProtocolTypeRouter({
    'http': URLRouter([
        url(r'^events/', 
            URLRouter(django_eventstream.routing.urlpatterns)
        , { 'channels': ['test'] }),
        url(r'', get_asgi_application()),
    ]),
})

image

image

if work with templates in django, it works very well without any error. but I want to use react at my frontend side. please let me know how to fix this error. I'm stuck at it very badly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions