SSE`this.ventSource.onMessage`调用失败。错误`事件源的响应具有不是文本/事件流的MIME类型(应用程序/json")。 [英] SSE `this.eventSource.onmessage` call fails. Error `"EventSource's response has a MIME type ("application/json") that is not "text/event-stream"

查看:21
本文介绍了SSE`this.ventSource.onMessage`调用失败。错误`事件源的响应具有不是文本/事件流的MIME类型(应用程序/json")。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ANGLE Server Sent事件this.eventSource.onmessage调用失败,错误"EventSource's response has a MIME type ("application/json") that is not "text/event-stream". Aborting the connection."我在Chrome Dev工具(附图)中看到有两个Content-Type被返回。

后端代码:SpringReader/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.

任何帮助都是最好的!

推荐答案

我使用ASP.Net(和NodeJS)时遇到了同样的问题。

我不知道这是否有帮助,但我体验到,如果您使用Moesif Origin & CORS Changer(在标准配置中,我没有测试自定义配置),某些标头会被插件(至少是Content-Type和X-Content-Type-Options)添加或覆盖(选择了一个)。

因此,可能是您在Chrome中安装的某个插件导致了这一问题。尝试在不同的浏览器中运行或不使用插件。

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

这篇关于SSE`this.ventSource.onMessage`调用失败。错误`事件源的响应具有不是文本/事件流的MIME类型(应用程序/json&quot;)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆