代码之家  ›  专栏  ›  技术社区  ›  Ying

JSON的回应太晚了

  •  0
  • Ying  · 技术社区  · 8 年前

    我有这样的网址:

    example.com/account/reset/6b183b5025d5b865963f4499f257705d

    我的脚本中有一个ajax提交,如下所示:

    form.ajaxSubmit({
                    url: '/account/reset',
                    success: function (result, status, xhr, $form) {
                        if (result.status === 'success') {
                            window.location.replace("/account/login");
                        }else{
                            btn.removeClass('m-loader m-loader--right m-loader--light').attr('disabled', false);
                            showErrorMsg(form, 'danger', result.message);
                            alert(result.status);
                        }
                    }
                });
    

    回答如下:

    {"status":"success","title":"Success!","message":"Password changed successfully please login to continue"}
    

    提交表单Ajax时,我得到了一个响应,该响应会触发一个“未定义”的警报,在该警报之后,我得到了一个状态为成功的JSON响应。我怎么得到2个答复?我怎么能在第一次反应之前捕捉到第二次反应呢?

    如果需要,以下是完整的JS:

    //== Class Definition
    let reset = function () {
    
        let reset = $('#m_login');
    
        let showErrorMsg = function (form, type, msg) {
            let alert = $('<div class="m-alert m-alert--outline alert alert-' + type + ' alert-dismissible" role="alert">\
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"></button>\
                <span></span>\
            </div>');
    
            form.find('.alert').remove();
            alert.prependTo(form);
            //alert.animateClass('fadeIn animated');
            mUtil.animateClass(alert[0], 'fadeIn animated');
            alert.find('span').html(msg);
        };
    
        let resetPassword = function () {
            $('#m_login_reset_submit').click(function (e) {
                e.preventDefault();
    
                let btn = $(this);
                let form = $(this).closest('form');
    
                form.validate({
                    rules: {
                        password: {
                            required: true,
                            minlength: 12
                        },
                        password_confirmation: {
                            required: true,
                            equalTo: '#password'
                        }
                    }
                });
    
                if (!form.valid()) {
                    return;
                }
    
                btn.addClass('m-loader m-loader--right m-loader--light').attr('disabled', true);
    
                form.ajaxSubmit({
                    url: '/account/reset',
                    success: function (result, status, xhr, $form) {
                        if (result.status === 'success') {
                            window.location.replace("/account/login");
                        }else{
                            btn.removeClass('m-loader m-loader--right m-loader--light').attr('disabled', false);
                            showErrorMsg(form, 'danger', result.message);
                            alert(result.status);
                        }
                    }
                });
            });
        };
    
    
        //== Public Functions
        return {
            // public functions
            init: function () {
                resetPassword();
            }
        };
    }();
    //== Class Initialization
    jQuery(document).ready(function () {
        reset.init();
        $(document).ajaxError(function (e, xhr, opt, thrownError) {
            swal({
                title: xhr.status + " " + xhr.statusText,
                //text: "Error requesting : " + opt.url +" "+ thrownError,
                text: xhr.responseText,
                type: "error",
                timer: 15000
            });
            $('#m_login_signin_submit').removeClass('m-loader m-loader--right m-loader--light').attr('disabled', false);
        });
    });
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Maya Animesh Shrivastav    8 年前

    undefined

    success: function (encoded, status, xhr, $form) {
        decoded = JSON.parse(encoded)
        if (decoded.status === 'success') {