Javascript. Странная ошибка с событиями сервера
Я довольно новичок в Javascript и у меня возникла странная ошибка при использовании Javascript Server Event с Django Framework. Проверьте код ниже.
Django: main_course_api.py
Эта функция возвращает событие сервера обратно на front-end.... (работает хорошо):
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);
}
});
Это должно работать, но это дает мне такую ошибку в консоли JS каждый раз после выполнения:
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
Похоже, что-то не так с асинхронным, но не совсем уверен в проблеме, хочу узнать идеи настоящих экспертов :D