[英]How to cancel the http request after 2 minutes in Javascript form submit?
是否有任何屬性可以在2分鍾后以Javascript形式提交來取消請求
JavaScript代碼
var document = $document[0];
var form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', '/service/'+url);
var hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', 'filterParam');
hiddenField.setAttribute('value',angular.toJson(input));
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();
就像HTTP配置對象中的超時。
$http({
method : 'POST',
**timeout : 120000,**
}).success(function(data) {
}).error(function(data, status) {
});
您可以使用window.stop
(在IE中為document.execCommand("Stop")
)來中止所有待處理的表單提交。 該代碼將是:
var document = $document[0];
var form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', '/service/'+url);
var hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', 'filterParam');
hiddenField.setAttribute('value',angular.toJson(input));
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();
setTimeout(function() {
try {
window.stop();
} catch (exception) {
document.execCommand('Stop');
}
}, 120000);
根據表單元素的DOM接口 ,沒有屬性或方法可以中止表單元素級別的表單提交。
這種方法的解決方案是從表單中刪除action
屬性,並在javascript方法內觸發ajax調用,您可以在其中手動處理超時。
就像是:
<form>
..
<button ng-click="submit()" />
</form>
$scope.submit = function(){
var promise= $q.defer();
$http.get('/actionUrl', {timeout: promise.promise})
.then(function(resp){
})
promise.resolve(); //cancel the request. you can set a timeout here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.