java SSE 'this.eventSource.onmessage'调用失败,错误'“EventSource的响应具有不是“text/event-stream”的MIME类型(“application/json”)

omjgkv6w  于 2022-12-17  发布在  Java
关注(0)|答案(2)|浏览(2100)

Angular服务器发送事件this.eventSource.onmessage调用失败,错误为"EventSource's response has a MIME type ("application/json") that is not "text/event-stream". Aborting the connection."我在Chrome开发工具(随附图片)中看到返回了两个Content-Type。x1c 0d1x

后端代码:SpringReact器/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.
任何帮助都很好!

ars1skjm

ars1skjm1#

我在使用ASP.NET(和nodeJS)时遇到了同样的问题。
我不知道这是否有帮助,但我经历过,如果你使用Moesif Origin & CORS Changer(在标准配置中,我没有测试自定义的),一些标题会被插件添加或覆盖(选择“新”的)(至少是内容类型和X内容类型选项),正如我们在你的开发工具屏幕截图中看到的。
所以可能是你在Chrome中安装的某个插件导致了这一点。尝试在不同的浏览器中运行或不使用插件。
希望我能帮上忙,祝你有愉快的一天!

amrnrhlw

amrnrhlw2#

我也遇到过同样的问题,在eventSource.onmessage的某个地方,代码将MIME类型值更改为{'content-type': 'application/json'},您必须保留它的值{'content-type':'text/event-stream'}

相关问题