如何在 toastr 通知后重定向

How can I redirect after toastr notification

我正在尝试在 toastr 通知完成显示后重定向。我目前有 ajax 个请求

 $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('value')
            },
            type: $(form).attr('method'),
            url: $(form).attr('action'),
            data: $(form).serialize(),
            dataType: 'json',
            success: function (data) {
                toastr.success('Hello','Your fun',{timeOut: 2000,preventDuplicates: true, positionClass:'toast-top-center'});


                     return window.location.href = '/';

            },
            error: function (data) {
                    var html = '<div class="alert alert-danger">Email/Password is invalid</div>';
                    $('#loginMsg').html(html);
            }

问题是它显示了通知,但很快重定向到实际阅读通知。如何仅在 toastr 通知隐藏后重定向?

toastr给出回调选项

toastr.options.onShown = function() { console.log('hello'); } toastr.options.onHidden = function() { console.log('goodbye'); } toastr.options.onclick = function() { console.log('clicked'); } toastr.options.onCloseClick = function() { console.log('close button clicked'); }

在函数内部您可以使用重定向 URL

这取决于您使用的插件check here

希望对您有所帮助

$.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('value')
        },
        type: $(form).attr('method'),
        url: $(form).attr('action'),
        data: $(form).serialize(),
        dataType: 'json',
        success: function(data) {
            toastr.success('Hello', 'Your fun', {
                timeOut: 2000,
                preventDuplicates: true,
                positionClass: 'toast-top-center',
                // Redirect 
                onHidden: function() {
                    window.location.href = '/';
                }
            });
        },
        error: function(data) {
            var html = '<div class="alert alert-danger">Email/Password is invalid</div>';
            $('#loginMsg').html(html);
        }
    });

//Before calling the toastr method, define the callback function to reload page or redirect to onother url

toastr.options.onHidden = function(){
  window.location.reload();
}

//Call de toast
toastr.error('Your massage here');