HTML5 Server-Sent Events:如何设置withCredentials选项?

10 javascript html5 server-sent-events

根据WHATWG -下面的服务器发送事件是使用EventSource接口的API :

[Constructor(DOMString url, optional EventSourceInit eventSourceInitDict)]
interface EventSource : EventTarget {
  readonly attribute DOMString url;
  readonly attribute boolean withCredentials;
  //....
};
Run Code Online (Sandbox Code Playgroud)

withCredentials属性必须返回上次初始化的值.创建对象时,必须将其初始化为false.

简单的例子:

var stocks = new EventSource("events.php");
stocks.onmessage = function (event) {
  //alert(event.data);
};
Run Code Online (Sandbox Code Playgroud)

现在,如何在此示例中包含或设置withCredentials?

Alo*_*hci 16

我没试过,但按照你链接的规格,我相信它会是这样的:

var stocks = new EventSource("events.php", { withCredentials: true });
Run Code Online (Sandbox Code Playgroud)

如果你转到http://www.w3.org/TR/WebIDL/#idl-exceptions然后向上滚动以查看上面的示例,你可以看到使用字典设置初始化值的类似模式.