1

我想使用 noty 插件在 ajax 调用期间显示消息。为此,我必须打开两个通知框,一个在 beforeSend 中,一个在成功回调中。

$('#insert').on('click', function() {
    $.ajax({
        type: "POST",
        url: action.php,
        beforeSend: function() {
            callNoty('Waiting...');
        },
        success: function(data) {
            callNoty('Insert ok');
        }
    });
});

JS

$(function() {
    callNoty(text) {
        var n = noty({
            layout: 'center',
            theme: 'relax',
            type: 'success',
            text: text,
            dismissQueue: true, // If you want to use queue feature set this true
            template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
            animation: {
                open: { height: 'toggle' }, // or Animate.css class names like: 'animated bounceInLeft'
                close: { height: 'toggle' }, // or Animate.css class names like: 'animated bounceOutLeft'
                //easing: 'swing',
                speed: 0 // opening & closing animation speed
            },
            timeout: 3000, // delay for closing event. Set false for sticky notifications
            force: false, // adds notification to the beginning of queue when set to true
            modal: true,
            maxVisible: 2, // you can set max visible notification for dismissQueue true option,
            killer: true, // for close all notifications before show
            closeWith: ['click', 'hover'], // ['click', 'button', 'hover', 'backdrop'] // backdrop click will close all notifications
            callback: {
                onShow: function() {},
                afterShow: function() {},
                onClose: function() {},
                afterClose: function() {},
                onCloseClick: function() {},
            },
            buttons: false // an array of buttons
        });
    };
});

我读到您可以即时编辑文本和类型,我会这样做,因此在 beforeSend 上打开一个通知框并在成功回调中更改类型和文本。我怎么能那样做?谢谢

4

1 回答 1

1

我想我迟到了五年,但您可以通过以下方式做到这一点:您必须将 noty.js 文件链接到根 html 文件夹。专注于成功和错误处理程序

            $.ajax({
                url : "/posts/create",
                method:"POST",
                data: newPostForm.serialize(), 
                success : function(data){
                    
                   new Noty({
                    theme : 'mint' , 
                    text: "Post Created",
                    type: 'alert',
                    layout : "topCenter",
                    timeout : 1500
                    
                    }).show();
                    
                   
                },
                error: function(error){
                    console.log(error.responseText);
                    new Noty({
                        theme : 'mint' , 
                        text: error.responseText,
                        type: 'alert',
                        layout : "topCenter",
                        timeout : 1500
                        
                        }).show();
                }
            });

因此,我们只需在 aja 调用的成功或错误处理函数中调用新的 Noty 函数,并使用您要显示的自定义和消息

于 2021-02-25T15:37:48.170 回答