Ajax帖子无法在窗口中运行beforeunload

时间:2016-12-13 22:55:39

标签: javascript jquery ajax onbeforeunload

我有多个相同的表格(每月一个),使用此脚本坐在多个部分:

http://www.jquery-steps.com/Examples

那里没有问题。

当我点击下一个或上一个按钮时,我会在表格上发布数据:



var tsf = $('.tsf-wizard-1').tsfWizard({
                stepEffect: 'basic',
                stepStyle: 'style1',
				 validation: false,
                navPosition: 'top',
                manySteps: true,
                stepTransition: true,
                showButtons: true,
                showStepNum: true,
                height: '500px',
                prevBtn: '<i class="fa fa-chevron-left"></i> Vorige Maand',
                nextBtn: 'Volgende Maand <i class="fa fa-chevron-right"></i>',
                finishBtn: '<i class="fa fa-table"></i> Overzicht',	
                onNextClick: function (e, from, to, validation) {
	                var stap = from+1;				
					var formstep = '#'+stap.toString()+'_form';
					var pathname = window.location.pathname; // Returns path only
					var url      = window.location.href;  
                    $.ajax({
						type: 'post',
						url: 'user_settings/add_data',
						data: $(formstep).serialize(),
						success: function () {
						  console.log($(formstep).serialize());
						},
						error: function (){
						   $( "#remarks").html("<span class=\"label label-danger\">ERROR !</span> Problem saving..");
						}
					});
                },	
				onPrevClick: function (e, from, to, validation) {
	                var stap = from+1;				
					var formstep = '#'+stap.toString()+'_form';
                    $.ajax({
						type: 'post',
						url: 'user_settings/add_data',
						data: $(formstep).serialize(),
						success: function () {
						 console.log($(formstep).serialize());
						},
						error: function (){
						   $( "#remarks").html("<span class=\"label label-danger\">ERROR !</span> Problem saving..");
						}
					});
                }								
            });
&#13;
&#13;
&#13;

但是,当用户离开页面时,我希望通过ajax调用发布所有表单。所以我尝试了这个(hold.open和.close是繁忙指标)

&#13;
&#13;
$(window).on('beforeunload', function ()
	{
		//this will work only for Chrome
		pageSave();
	});

	$(window).on("unload", function ()
	{
		//this will work for other browsers
		pageSave();
});	



function pageSave()
	{
		if (!_wasPageSaved)
		{
			HoldOn.open({
					theme:'sk-cube-grid',
					message:"<h4>Savig data..</h4>"
				});
				var goodsaves=0;
				for (i = 1; i <= 12; i++) { 
					var formstep = '#'+i.toString()+'_form';
					$.ajax({
						type: 'post',
						url: '<?php echo base_url();?>user_settings/add_data',
						data: $(formstep).serialize(),
						success: function () {
					      goodsaves=goodsaves+1;
						},
						error: function (){
							console.log('error saving data :');
						}
					});
				}
			HoldOn.close();
			if (goodsaves=>11) {
				_wasPageSaved = true;
			     console.log('all forms saved');
			}
        }
    }
&#13;
&#13;
&#13;

问题是有时会保存数据,有时则不会。我不能指责它为什么它总是不起作用。我看到忙碌的指示器,我在控制台中没有收到任何警报,下一页已加载。但是我的数据没有保存......

希望任何人都能清除这一点,谢谢提前分配。

0 个答案:

没有答案