服务器可以通过 POST 发送带有 EventSource 传递参数的事件 (SSE) [英] Can Server Sent Events (SSE) with EventSource pass parameter by POST

查看:43
本文介绍了服务器可以通过 POST 发送带有 EventSource 传递参数的事件 (SSE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Html5 服务器发送的事件.服务器端是Java Servlet.我有一个 json 数组数据要传递给服务器.

I'm using Html5 Server Sent Events. The server side is Java Servlet. I have a json array data wants to pass to server.

var source = new EventSource("../GetPointVal?id=100&jsondata=" + JSON.stringify(data));

如果数组很小,服务器端可以得到查询字符串.但是如果数组大小很大.(可能超过数千个字符),服务器无法获取查询字符串.是否可以在 new EventSource(...) 中使用 POST 方法将 json 数组传递给服务器,以避免查询字符串长度限制?

If the array size is small , the server side can get the querystring. But if the array size is big. (maybe over thousands of characters), the server can't get the querystring. Is it possible to use POST method in new EventSource(...) to to pass the json array to server that can avoid the querystring length limitation?

推荐答案

不,SSE 标准不允许 POST.

No, the SSE standard does not allow POST.

(没有技术原因,据我所知 - 我认为只是设计人员从未见过用例:这不仅仅是大数据,而是如果您想进行自定义身份验证方案出于安全原因不将密码放入 GET 数据中.)

(For no technical reason, as far as I've been able to tell - I think it was just that the designers never saw the use cases: it is not just large data, but if you want to do a custom authentication scheme there are security reasons not to put the password in GET data.)

XMLHttpRequest(即 AJAX)确实允许 POST,因此一种选择是返回到旧的 long-poll/comet 方法.(我的书 Data Push Apps with HTML5 SSE 详细介绍了如何做这个.)

XMLHttpRequest (i.e. AJAX) does allow POST, so one option is to go back to the older long-poll/comet methods. (My book, Data Push Apps with HTML5 SSE goes into quite some detail about how to do this.)

另一种方法是将所有数据预先POST,并存储在一个HttpSession中,然后调用SSE进程,该进程可以利用该会话数据.(SSE 确实支持 cookie,所以 JSESSIONID cookie 应该可以正常工作.)

Another approach is to POST all the data in beforehand, and store it in an HttpSession, and then call the SSE process, which can make use of that session data. (SSE does support cookies, so the JSESSIONID cookie should work fine.)

附言标准 没有明确说明不能使用 POST.但是,与 XMLHttpRequest 不同的是,没有参数指定要使用的 http 方法,也没有办法指定要发布的数据.

P.S. The standard doesn't explicitly say POST cannot be used. But, unlike XMLHttpRequest, there is no parameter to specify the http method to use, and no way to specify the data you want to post.

这篇关于服务器可以通过 POST 发送带有 EventSource 传递参数的事件 (SSE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆