简体   繁体   English

提交表单后,Ajax请求不完整

[英]Incomplete Ajax Request after Form Submission

I am currently running a mobile-focused site that supports file upload and what I am attempting to do is save the user a lot of idle time when they upload a file to a form. 我当前正在运行一个支持移动设备的网站,该网站支持文件上传,而我试图做的就是在用户将文件上传到表单时为用户节省了大量的空闲时间。

My goal is as follows: 我的目标如下:

 xhr = new XMLHttpRequest();

 xhr.open("POST", "http://myServer/myEndpoint");
 xhr.setRequestHeader("Cache-Control", "no-cache");
 xhr.setRequestHeader('Content-Type', 'text');
 xhr.setRequestHeader('foo', "bar");
 xhr.send( myData );

 //PseudoCode
 if( xhr.DataIsSent ) myForm.submit();

As you can see, I'm aiming to prevent the user having to wait for an ajax response before submitting the form, Ideally i'd like this to run in the background irrespective of what the user does. 如您所见,我的目标是防止用户在提交表单之前必须等待ajax响应。理想情况下,无论用户做什么,我都希望它在后台运行。

My understanding of ajax was that once the request is dispatched, unless it is manually cancelled, it will be completed, however, I have had cases where the data which reaches the server is incomplete 我对ajax的理解是,一旦调度了请求,除非手动将其取消,否则它将完成,但是,我遇到了到达服务器的数据不完整的情况

Any Suggestions on how i could achieve this reliably would be greatly appreciated. 我将如何可靠地实现这一目标的任何建议将不胜感激。 So far it appears that once the form is submitted, the partially completed request is terminated. 到目前为止,似乎已经提交了表单,但部分完成的请求已终止。

As i can read on the Mozilla Developer Pages, XmlHttpResponses are stateful, as well. 正如我在Mozilla开发人员页面上所读到的那样,XmlHttpResponses也是有状态的。

I would recommend you to use jQuery: 1. create a promise for your ajax request 2. use when().then() before the Form submit 3. catch errors 我建议您使用jQuery:1.为ajax请求创建一个Promise 2.在Form提交之前使用when()。then()3.捕获错误

Do you need any code example for it? 您是否需要任何代码示例?

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

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