Perform AJAX request before continuing with default action [closed]

做~自己de王妃 提交于 2019-12-13 11:30:03

问题


I have a basic AJAX request like so:

    $.ajax({
        url: 'check_password.php',
        type: 'post',
        data: 'password='+password,
        dataType: 'json',
        success: function(json) {
            if (json['success']) {
               //Continue with default action
            }else{
                //Password is invalid, stop the script and return an error
                preventDefault();
                $(".error").show();
            }
        }

回答1:


I'd suggest instead preventing the default completely, then submitting the form (if it is a form) by using .submit() on the form node, thus bypassing the jQuery event handler.

$("#myform").on("submit",function(event){
    event.preventDefault();
    var form = this;
    $.ajax({
        url: theurl,
        type: "POST",
        data: thedata
        dataType: "json"
    }).done(function(json){
        if (json.success) {
            form.submit();
        }
        else {
            $("#error").show();
        }
    });    
});


来源:https://stackoverflow.com/questions/15033991/perform-ajax-request-before-continuing-with-default-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!