问题
While in other questions people claimt EventSource is fairly well documented I have found it to be more implied then explicit in some cases.
My understanding is that when you initialise an EventSource object in JS it opens a connection to your server using the specified URI.
Is this connection initiated using GET?
(Not sure if this constitutes a second question) Is it possible to use/force another HTTP Method (POST)?
回答1:
The request method when using the EventSource
interface is a GET
request. You can include a query string in the URL passed to the constructor and parse the query string at the server.
const stream = "data: event stream\n\n";
const blob = new Blob([stream], {type:"text/event-stream"});
const blobURL = URL.createObjectURL(blob);
const es = new EventSource(blobURL);
es.onmessage = e => {
console.log(e.data);
}
es.onerror = e => {
es.close();
}
来源:https://stackoverflow.com/questions/48372650/what-http-method-does-eventsource-use-to-open-a-connection