1

Angular Server Sent Event调用失败,我在 Chrome 开发工具(附图片)中看到this.eventSource.onmessage一个错误,返回了两个。"EventSource's response has a MIME type ("application/json") that is not "text/event-stream". Aborting the connection."Content-TypeChrome 开发工具响应网络选项卡

后端代码:Spring Reactor/REST

    @GetMapping(value="/events",produces = "text/event-stream;charset=UTF-8")
    public Flux<ConsumerEvent> getProductEvents(){
        return kafkaService.getReceiverRecordAllFlux()
                .map(record->
                    new ConsumerEvent(record.topic(),record.value())
                );
        }
}

前端:角

public startKafkaTopicInfoEventSource(): void {
    let url = BASE_URL;
    this.eventSource = new EventSource(url); 
    this.eventSource.onmessage = (event) => {//Error: EventSource's response has a MIME type ("application/json") that is not "text/event-stream". Aborting the connection
        this.zone.run(() => {
        // some code here
      })

    }
// other code here
}

方法this.eventSource.onmessage报错EventSource's response has a MIME type ("application/json") that is not "text/event-stream". Aborting the connection.

任何帮助都会很棒!

4

1 回答 1

1

我在使用 ASP.NET(和 nodeJS)时遇到了同样的问题。

我不知道这是否有帮助,但我经历过,如果您使用Moesif Origin & CORS Changer(在标准配置中,我没有测试自定义的)一些标题会被添加或覆盖(选择“新”的)正如我们在您的开发工具屏幕截图中看到的那样,通过插件(至少 Content-Type 和 X-Content-Type-Options)。

因此,也许您在 Chrome 中安装的某些插件会导致这种情况。尝试在不同的浏览器中运行或不使用插件。

希望我能帮助别人,祝你有美好的一天!

于 2021-03-10T11:22:45.050 回答