eventsource post
时间: 2023-10-11 11:08:42 浏览: 89
HTML通信API跨文档消息传输-Html5ppt 资料
EventSource is a web API that allows a web page to receive real-time updates from a server. It uses a long-lived HTTP connection to push events from the server to the client. However, EventSource only supports GET requests and cannot handle POST requests.
If you want to send data from the client to the server using EventSource, you can include the data as query parameters in the URL of the EventSource endpoint. For example, you can send data to the server like this:
```
new EventSource("/events?data=some_data");
```
On the server side, you can access the data using the query parameters. For example, in Node.js, you can access the data like this:
```
const http = require("http");
http.createServer((req, res) => {
if (req.url === "/events") {
const data = req.url.query.data;
// Do something with the data
}
}).listen(3000);
```
Note that sending sensitive data as query parameters may not be secure and may be visible in server logs. In such cases, it may be better to use other techniques such as WebSockets or AJAX to send data.
阅读全文