[英]Ajax detect when from submit is complete
我想知道我的節點服務器何時響應我的ajax提交。 目前,表單通過幾個字符串發送到服務器,在那里找到文件並將其發送回客戶端。 我希望能夠檢測客戶端何時收到文件,以便我可以更改網頁。 我曾嘗試使用Ajax Form,但我認為它不支持接收文件,只支持XML或JSON響應等。當我嘗試它時,節點服務器會做正確的事情並發送文件,但客戶端永遠不會收到它。 這是我當前的代碼,並且客戶端收到該文件,但我不能在收到它后不能更改網頁。
$(document).on('click', 'a', function () {
var form = $(this).parents().find("#download-form").submit();
$(this).addClass("disabled");
$(this).html("<i class='fa fa-refresh fa-spin'></i>");
});
您可以將表單的target
屬性設置為iframe,並將事件偵聽器添加到iframe的load
事件中。
示例:(也在jsfiddle中 )
<script>
document.getElementById("resultFrame").addEventListener("load", function(){
// do something once iframe loaded
document.getElementById("resultCount").innerHTML = parseInt(document.getElementById("resultCount").textContent)+1;
});
</script>
<form method="GET" action="https://fiddle.jshell.net/img/logo.png" target="resultFrame">
<input type="submit" />
</form>
<p id="resultCount">0</p>
<iframe name="resultFrame" id="resultFrame"></iframe>
您可以使用ajax:complete
事件來檢測表單提交成功()
var form = $(this).parents().find("#download-form");
form.bind('ajax:complete', function() {
// tasks to do
});
form.submit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.