简体   繁体   English

JavaScript API查询当前state的表单提交

[英]JavaScript API to query current state of form submission

Does any browser support a JS API to query the status of submitting a form?有没有浏览器支持JS API 来查询提交表单的状态? I would like to add generic code to our application that monitors any submitted form and displays a progress bar for the form.我想向我们的应用程序添加通用代码,以监控任何提交的表单并显示表单的进度条。 Origin server cannot report the status if there's buffering reverse proxy between client and the origin server (eg Cloudflare) so I would need something supported by the client.如果客户端和源服务器(例如 Cloudflare)之间存在缓冲反向代理,源服务器无法报告状态,所以我需要客户端支持的东西。

Assume that I run code document.getElementById("myForm").submit();假设我运行代码document.getElementById("myForm").submit(); . . Is there any way to figure out how much of the form has been submitted?有什么办法可以知道已经提交了多少表格? The client obviously knows the exact byte count it will be sending so it should be able to report how much (count of bytes or a percentage) of the full transfer has been pushed to net socket.客户端显然知道它将发送的确切字节数,因此它应该能够报告已将完整传输的多少(字节数或百分比)推送到网络套接字。 I understand that this is not the same as getting the data to the origin server but I'm not interested on that.我知道这与将数据发送到源服务器不同,但我对此不感兴趣。 I would like to support long forms (lots of text over very slow internet connection) and forms with huge files (files bigger than 2 GB over fast internet connection).我想支持长 forms(在非常慢的互联网连接上的大量文本)和带有大文件的 forms(通过快速互联网连接的文件大于 2 GB)。

Update: I asked about this on WHATWG mailing list .更新: 我在 WHATWG 邮件列表中询问了这个问题 I believe the best answer this far is https://stackoverflow.com/a/28386477/334451 which does not deal great with HTTP 303 but should be otherwise okay.我相信到目前为止最好的答案是https://stackoverflow.com/a/28386477/334451与 HTTP 303 没有什么关系,但应该没问题。

There's no way to do this without using using AJAX/XHR via JavaScript and listening for progress events that way.如果不通过 JavaScript 使用 AJAX/XHR 并以这种方式监听progress事件,就无法做到这一点。

This will probably never be implemented for native form submission because it would allow more accurate timing measurements for response time side-channel attacks.这可能永远不会在本机表单提交中实现,因为它将允许对响应时间侧信道攻击进行更准确的计时测量。 See explanation by Anne van Kesteren for details.有关详细信息,请参阅Anne van Kesteren 的解释

Because AJAX/XHR cannot submit to cross-origin servers, it's okay to get accurate timing for those submissions.因为 AJAX/XHR 不能提交到跨域服务器,所以可以为这些提交获取准确的时间。 However, native forms can submit cross-origin and there even the timing for any possible redirects could leak information about the remote cross-origin server.但是,本机 forms 可以提交跨域,甚至任何可能的重定向时间都可能泄漏有关远程跨域服务器的信息。 As exposing the timing for native submission would leak more data than existing APIs, this will not be added to native submission API.由于暴露原生提交的时间会比现有 API 泄漏更多的数据,因此不会将其添加到原生提交 API。

You can handle the existing form like this:您可以像这样处理现有表单:

form = ... ; // the HTMLFormElement you want to monitor for submission

form.addEventListener('submit', function (e)
{
    let xhr = new XMLHttpRequest();
    xhr.upload.addEventListener('loadstart', function ..., { capture: false, passive: true });
    xhr.upload.addEventListener('progress', function ..., { capture: false, passive: true });
    xhr.addEventListener('readystatechange', function ..., { capture: false, passive: true });
    xhr.upload.addEventListener('error', function ..., { capture: false, passive: true });
    xhr.upload.addEventListener('abort', function ..., { capture: false, passive: true });
    xhr.upload.addEventListener('load', function ..., { capture: false, passive: true });
    xhr.addEventListener('readystatechange', function ..., { capture: false, passive: true });

    data = new FormData(form);
    // data.append(key, value); // if you want to add extra data
    xhr.send(formdata);

    e.preventDefault();
}

And yes, you need all those event listeners to be compatible with all the browsers and to handle all edge cases and to get all possible timing.是的,您需要所有这些事件侦听器与所有浏览器兼容并处理所有边缘情况并获得所有可能的时间。 And you'll have to create the UI to explain the user about failing network connection, timeouts, etc etc that the browser typically takes care of.而且您必须创建 UI 来向用户解释浏览器通常会处理的网络连接失败、超时等问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM