简体   繁体   English

HTTP表单提交回调

[英]HTTP form submit callback

I'm posting a form that causes a file to be downloaded. 我正在发布一个导致下载文件的表格。 So my browser page does not change. 所以我的浏览器页面没有改变。 My form is eg form action="/download/file.txt" . 我的表单是例如form action="/download/file.txt" I'm aware that I could use JQuery and download the file with AJAX and use the JQuery callback mechanisms. 我知道我可以使用JQuery并通过AJAX下载文件并使用JQuery回调机制。 However, I don't want to use JQuery because this is a learning experience. 但是,我不想使用JQuery,因为这是一种学习经验。 Is there any way to call a Javascript function when my file has downloaded? 下载文件后,有什么方法可以调用Javascript函数?

There is no way to do this without AJAX. 没有AJAX,就无法做到这一点。 So either you use JQuery's AJAX call, or just plain JS, and use the onreadystatechange event ( http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp ). 因此,您可以使用JQuery的AJAX调用,也可以仅使用普通的JS,然后使用onreadystatechange事件( http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp )。 There is no JS event you can trigger when the submit is "completed" by setting it as an attribute on the form (like onClick="" etc). 通过将提交设置为表单上的属性(例如onClick =“”等),可以在提交“完成”时触发任何JS事件。

So plain JS is possible with AJAX, but you'd just be making it yourself harder 因此,使用AJAX可以使用普通的JS,但您自己会更难

you can use 您可以使用

<form action="nextStep.php" onsubmit = "return function(){
                 //download the file here **download/file.txt**
                 //if(downloaded == successful)
                 // set the flag as true
                 //else
                 // set the flas as false

}" >

if it is good then go the nextStep.php file 如果很好,请转到nextStep.php文件

The answer here uses an iframe to load the static file and get the notification in its onload event. 答案在这里使用iframe来加载静态文件,并得到其onload事件的通知。 You can check the same. 您可以检查相同。

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

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