【问题标题】:Ajax show message on redirectAjax 在重定向时显示消息
【发布时间】:2017-04-27 14:58:24
【问题描述】:

在我的 ajax 调用中,如果成功,我想重定向到另一个 URL 并显示由 toastr 形成的消息。

这是我的 ajax:

var redirectUrl = '@Url.Action("Index", "Informations")';

bootbox.confirm({
    title: "Delete?",
    message: "Are you sure you want to delete this?",
    buttons: {
        cancel: {
            label: '<i class="fa fa-times"></i> Cancel'
        },
        confirm: {
            label: '<i class="fa fa-check"></i> Confirm'
        }
    },
    callback: function(result) {
        if (result) {
            toastr.options = {
                timeOut: 5000
            }

            $.ajax({
                url: "/api/Informations/" + btn.attr("data-id"),
                method: "DELETE",
                success: function () {
                    window.location.href = redirectUrl;
                    toastr.success("Successfully deleted");
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    var status = capitalizeFirstLetter(textStatus);

                    toastr.error(status + " - " + errorThrown, "Sorry, something went wrong.");
                 }
             });
         }
      }
  });

现在问题肯定是调用 toastr.success 方法之前的重定向.. 那么如何在新页面上显示成功消息?

【问题讨论】:

    标签: jquery ajax toastr


    【解决方案1】:

    您可以使用 toastr 提供的回调,可能是 onclickonHidden

    toastr.options.onShown      = function() { window.location.href = redirectUrl; }
    toastr.options.onHidden     = function() { window.location.href = redirectUrl; }
    toastr.options.onclick      = function() { window.location.href = redirectUrl; }
    toastr.options.onCloseClick = function() { window.location.href = redirectUrl; }
    

    一个例子:

    $('.hej').click(function(e) {
      e.preventDefault();
      toastr.options.onHidden = function() { console.log('I\'m hidden'); }
      toastr.options.onclick = function() { console.log('You clicked me'); }
      toastr.success('Have fun storming the castle!', 'Miracle Max Says');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://codeseven.github.io/toastr/build/toastr.min.js"></script>
    <link href="http://codeseven.github.io/toastr/build/toastr.min.css" rel="stylesheet"/>
    
    <a href="#" class="hej">Click me!</a>

    在你的情况下,它可能是这样的:

    <script>
      ...
      $.ajax({
        url: "/api/Informations/" + btn.attr("data-id"),
        method: "DELETE",
        success: function () {
          toastr.options.onHidden = function() { window.location.href = redirectUrl; }
          toastr.success("Successfully deleted");
        },
        error: function(jqXHR, textStatus, errorThrown) {
          var status = capitalizeFirstLetter(textStatus);
          toastr.error(status + " - " + errorThrown, "Sorry, something went wrong.");
         }
      });
      ...
    </script>
    

    更新:如果您想要在新页面中显示一条消息,您已经重定向,那么您可以通过 url 将该消息作为参数传递:

    toastr.options.onHidden = function() { window.location.href = redirectUrl + '?message=any_message'; }
    

    而且我意识到必须在分配 toastr.success 之前设置选项。

    【讨论】:

    • 我已将我的代码编辑为您提供的内容,成功消息后我没有被重定向
    • 由于某些原因,JSFiddle 在必须重定向时无法到达网站,但请检查此pen 并检查控制台,它应该可以在您的本地计算机上进行测试。
    • 为了清楚起见,我希望这条消息出现在我们重定向到的页面上,但我仍然没有被重定向
    • 然后您需要将该消息作为参数传递给您重定向的 url。查看更新后的答案。
    • 首先,我只想在消息出现后被重定向。我根本没有被重定向
    猜你喜欢
    • 2016-10-09
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2017-07-06
    相关资源
    最近更新 更多