weixin_33682719 2017-09-26 06:21
浏览 36

如果需要,请重新提交ajaxForm

I am using ajaxForm to send data to php file which runs in background, and some js functions will be checking if php file is running asynchroniously. So in case of some failure(internet/electricity went off), I want my user be able to retry the last form submission. So I found some ajax retry functions as this, best way to retry ajax but it is on the same ajax request, i need to be able to retry my ajax from another function. Is it even possible? Given that I am storing passed values in form.

$(document).ready(function(){
$('#form').ajaxForm({
    target: '#ajax-response',
    success: function(){
    initAjaxCheck();
     }
});
Check();
});

So, basically lockcheck() checks if process is still runing after page refresh/reload, and if any failure was detected with prevous run, it should be able to retry the form submission.

Here's js scripts:

function Check(){
    $.ajax({
    type:"POST",
        url:"ajax.php",
        data:{'flag':'Check'},
        success:function(result){
            if (result=="1") {
            initAjaxCheck();
            } 
        }
    });
}



function initAjaxCheck(){
    $.ajax({
        type:"POST",
        url:"ajax.php",
        data:{'flag':'initProcCheck'},
        success:function(data){
            if (data=="1"){ 
        timerCheck=setInterval(function(){
            ajaxCheck(timerCheck);
        },1000);
            } else { //unsuccessful
            var toRepeat = confirm ("Last try was unsuccessful. Retry?");
            if (toRepeat){alert("retry");} else {alert("cancel");}
            }
        } 
    });
}

So if my user press RETRY, I should have to retry my ajaxForm submission.

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33713350 2017-09-26 10:34
    关注

    I'm a little confused with what exactly you are asking, however I believe I somewhat understand.

    Trigger a form submission.

    $('#form').trigger("submit");
    

    or

    var event = jQuery.Event("submit");
    $('#form').trigger(event);
    

    To implement this in your code, simply add it after your confirm function.

    var toRepeat = confirm ("Last try was unsuccessful. Retry?");
    
    if (toRepeat){
        alert("retry");
        $('#form').trigger("submit");
    } else {
        alert("cancel");
    }
    
    评论

报告相同问题?