首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSE `this.eventSource.onmessage`调用失败。错误`“事件源的响应具有一个MIME类型("application/json"),而不是”text/事件流“。

SSE `this.eventSource.onmessage`调用失败。错误`“事件源的响应具有一个MIME类型("application/json"),而不是”text/事件流“。
EN

Stack Overflow用户
提问于 2020-05-01 16:38:42
回答 1查看 2.7K关注 0票数 2

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

后端代码:弹簧反应堆/REST

代码语言:javascript
复制
    @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())
                );
        }
}

前端:角

代码语言:javascript
复制
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.

任何帮助都会很好!

EN

回答 1

Stack Overflow用户

发布于 2021-03-10 11:22:45

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

我不知道这是否有帮助,但我经历过,如果您使用Moesif源& CORS变送器 (在标准配置中,我没有测试自定义的),一些标头会被插件(至少是内容类型和X内容类型选项)添加或覆盖(“新的”标题被选中),就像我们在Dev工具屏幕中看到的那样。

所以可能是你在Chrome上安装的插件造成了这种情况。尝试在不同的浏览器或没有插件的情况下运行。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61546705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档