HTML5 Server-Sent Events: How to set withCredentials option?

淺唱寂寞╮ 提交于 2021-02-18 05:21:28

问题


According to WHATWG - Server-Sent Events below is the API for using EventSource interface:

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

The withCredentials attribute must return the value to which it was last initialized. When the object is created, it must be initialized to false.

Simple example:

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

Now, how to include or set withCredentials in this example?


回答1:


I've not tried it, but going by the spec you link to, I believe it would be like this:

var stocks = new EventSource("events.php", { withCredentials: true });

If you go to http://www.w3.org/TR/WebIDL/#idl-exceptions then scroll up to see the example immediately above that, you can see a similar pattern of using a dictionary to set initialization values.



来源:https://stackoverflow.com/questions/16266493/html5-server-sent-events-how-to-set-withcredentials-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!