Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
Javascript. Weird Error with Server events

I'm pretty new in Javascript and having some weird error using Javascript Server Event with Django Framework. Check Code down below.

Django: main_course_api.py

This func returns server event back to front-end.... (working well):

def send_data_as_stream_event(request):
    import json
    request_data = request.GET.get('list_of_courses')

    append_to_list(request_data)
    course_data = get_parsed_data(LIST_OF_COURSES['list_of_courses'])

    response = django.http.StreamingHttpResponse(json.dumps(course_data))
    response['Content-Type'] = 'text/event-stream'

    return response

urls.py

    path('get/stream/response/data/', main_course_api.send_data_as_stream_event, 
    name='stream'),

Main.js

var evtSource = new EventSource('http://127.0.0.1:8000/get/stream/response/data/');

evtSource.onopen = function(event){
  console.log('connected....')
 }

evtSource.onmessage = function(event) {
console.log('on message event..', event)
console.log(event);
}

evtSource.onerror = function(err) {
console.error("EventSource failed:", err);
evtSource.close();
};

evtSource.onclose = function(code){
evtSource.close();
console.log('event source has been closed', code);
}
});

It is supposed to work, but it gives me such error in JS console every time after execution:

main.js:74 EventSource failed: Event {isTrusted: true, type: 'error', target: EventSource, 
currentTarget: EventSource, eventPhase: 2, …}isTrusted: truebubbles: falsecancelBubble: f 
falsecancelable: falsecomposed: falsecurrentTarget: EventSource {url: 
'http://127.0.0.1:8000/get/stream/response/data/', withCredentials: false, readyState: 2, 
onclose: ƒ, onopen: ƒ, …}defaultPrevented: falseeventPhase: 0path: []returnValue: 
truesrcElement: EventSource {url: 'http://127.0.0.1:8000/get/stream/response/data/', 
withCredentials: false, readyState: 2, onclose: ƒ, onopen: ƒ, …}target: EventSource {url: 
'http://127.0.0.1:8000/get/stream/response/data/', withCredentials: false, readyState: 2, 
onclose: ƒ, onopen: ƒ, …}timeStamp: 3608.300000011921type: "error"[[Prototype]]: Event
evtSource.onerror @ main.js:74

error (async)
(anonymous) @ main.js:73

Looks like something wrong with asynchronous, but not really sure about the problem, wanna know the ideas of real experts :D

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda