Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!