简体   繁体   English

SSE未捕获错误:SECURITY_ERR:DOM异常18,其中服务器提供服务器发送事件(SSE)

[英]SSE Uncaught Error: SECURITY_ERR: DOM Exception 18 with a server that provides Server Sent Events (SSE)

I am trying to connect browsers with a server that provides Server Sent Events (SSE). 我正在尝试将浏览器与提供服务器发送事件(SSE)的服务器连接。 This server has a different domain than the original one. 此服务器具有与原始服务器不同的域。 For example if you call http://d1.example.com/page this page will try to connect to an SSE channel on http://d2.example.com/subscribe . 例如,如果您致电http://d1.example.com/page此页面将尝试连接到http://d2.example.com/subscribe上的SSE频道。 Trying to do that will prompt the following error: 尝试这样做会提示以下错误:

Uncaught Error: SECURITY_ERR: DOM Exception 18

on the line: 在线上:

var source = new EventSource("http://d2.example.com/subscribe")

How can I fix that? 我该如何解决这个问题?

Update (Solutions that I have tried out): 更新(我尝试过的解决方案):

1- CORS 1- CORS

I tried CORS by adding Access-Control-Allow-Origin:* to the headers of my web service d2.example.com . 我通过将Access-Control-Allow-Origin:*到我的Web服务d2.example.com的标d2.example.com尝试CORS It did not solve EventSource problem, even though $.get("http://d2.example.com") calls from d1.example.com pages are working fine now! 它没有解决EventSource问题,即使来自d1.example.com页面的$.get("http://d2.example.com")调用现在工作正常! I thought SSE works on normal HTTP requests, so why is it not working on Chrome? 我认为SSE适用于普通的HTTP请求,为什么它不适用于Chrome?

2- Redirect 2-重定向

I am using httpd server, so I created a redirect rule in d1.example.com virtual host that passes SSE requests to d2.example.com . 我正在使用httpd服务器,因此我在d1.example.com虚拟主机中创建了一个重定向规则,将SSE请求传递给d2.example.com It worked perfectly with Firefox. 它与Firefox完美配合。 Chrome on the other hand did not prompt any error and did not connect to the SSE server either. 另一方面,Chrome没有提示任何错误,也没有连接到SSE服务器。 It looks like it dumped the whole EventSource command. 看起来它倾倒了整个EventSource命令。 It looks like this solution will never work, so lets move on... 看起来这个解决方案永远不会起作用,所以让我们继续......

3- Reverse proxy 3-反向代理

Both browsers connected on d1.example.com/subscribe which is basically connecting to d2.example.com through reverse proxy. 两个浏览器都连接在d1.example.com/subscribe ,它基本上通过反向代理连接到d2.example.com But on_close event is never caught, even if the browser is closed. 但是,即使浏览器已关闭, on_close事件也永远不会被捕获。 Which makes sense since the d2 server is now making the channel with the proxy server. 这是有道理的,因为d2服务器现在正在使用代理服务器建立通道。 How can I forward the on_close event from proxy server to d2? 如何将on_close事件从代理服务器转发到d2?

Is there any different ways that could make this work? 是否有任何不同的方法可以使这项工作?

The odds are high this is an issue of cross-origin resource sharing, which you must enable on the relevant domains. 可能性很高这是跨源资源共享的问题,您必须在相关域上启用它。

http://enable-cors.org/ http://enable-cors.org/

As the MDN says: 正如MDN所说:

Note: Although it's not yet part of the standard, EventSource supports CORS in Firefox 11 and later. 注意:虽然它还不是标准的一部分,但EventSource在Firefox 11及更高版本中支持CORS。 This is expected to become standard soon. 预计很快就会成为标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM