2

我正在尝试从通过 AJAX 检索的 JSON 中获取要显示的 toastr 消息。这必须能够更改警报的类型及其内容。我对 JSON 不太聪明,在阅读了一段时间后,我仍然不知道从哪里开始。任何指针?

阿贾克斯:

 function ping(data1)
    {
        $.ajax({
           type: "POST",
           url: "bridge/ping.php",
           data: "var1="+data1,
           success: 
        }
     });

烤面包机:

          toastr.success("Message here","Title here)
4

3 回答 3

4

基本上在您的 PHP 端,您将发送回一个编码的 JSON,例如:

$arr = array('message' => 'your message here', 'title' => 'your title here');
echo json_encode($arr);

现在,在您的客户端上,您编写success

success: function(data) {
    toastr.success(data.message, data.title);
}
于 2013-12-13T14:38:26.470 回答
1

看看我的问题:在 JSON 数组中,将“t”替换为不同的 toast 类型 ['info']、['warning']、['success'] 等。然后,解析 JSON 以适合 John Papa 的答案。

在 toastr 中使用超时

于 2014-01-27T02:30:36.047 回答
1

我像这样使用它。它工作完美。

服务器

$message = array('message' => 'Success!', 'title' => 'Updated');
return response()->json($message);

客户

success:function(data){
  setTimeout(() => {
  toastr.success(data.message, data.title);
  },500)
},
于 2020-12-12T12:29:28.180 回答