如何显示带有动态变量的 toastr 消息

我正在使用 Toastr 在 UI 中显示消息弹出窗口。


我通过 Ajax 向服务器发送请求,作为响应,我发送以下响应


echo json_encode(

                    array(

                            "type" => "error",

                            "message" => $error,

                            "status" => "Error While Updating!"

                         )

                );

我正在使用 resp.type 来显示动态 toastr 所以下面是我的 toastr 代码


.done(function(resp)

    {

        toastr.resp.type(resp.message, resp.status,{progressBar:!0,showMethod:"slideDown",hideMethod:"slideUp",timeOut:2e3,preventDuplicates: true,positionClass: "toast-bottom-right"});

    });

上面代码的问题是,当代码运行时,它会抛出一个错误消息Uncaught TypeError: toastr.type is not a function


任何人都可以帮我解决问题所在或此处可能是正确的解决方案


暮色呼如
浏览 60回答 1
1回答

慕的地10843

你不能嵌入toastr.resp.type,它是无效的,因此会抛出一个错误。据我了解,下面的代码可以按您的意愿工作.done(function(resp)   {       toastr[resp.type](resp.message, resp.status,{progressBar:!0,showMethod:"slideDown",hideMethod:"slideUp",timeOut:2e3,preventDuplicates: true,positionClass: "toast-bottom-right"});   });请将此视为参考:https ://github.com/CodeSeven/toastr/issues/203function showToast(message, timeout, type) {      type = (typeof type === 'undefined') ? 'info' : type;      toastr.options.timeOut = timeout;      toastr[type](message);  }showToast('Hello Toastr!", 15000, 'warning');
打开App,查看更多内容
随时随地看视频慕课网APP