【问题标题】:Bootstrap modal hidden event reload the body via ajaxBootstrap modal隐藏事件通过ajax重新加载body
【发布时间】:2017-04-07 23:46:19
【问题描述】:

我有一个弹出模式的页面并使用hidden.bs.modal 事件,我想通过ajax 重新加载相同的页面内容,即body 标签html。我有以下代码:

$("#actions-modal").on('hidden.bs.modal', function(){
        alert(document.location)
        $.ajax({
            url: document.location,
            type: "get",
            sucess: function(data){
                alert('hhhh')
                return $("#body1").html($(data).find('#body1'));
            },
            error: function(xhr){
                console.log(xhr);
            }
        })
    })

在上面的代码中,alert(document.location) 工作正常。但是,ajax 的success 处理程序中的alert('hhhh') 和ajax 的错误处理程序中的console.log(xhr) 根本不起作用。即没有success 也没有error!此外,浏览器的控制台没有任何错误。

【问题讨论】:

    标签: javascript jquery ajax twitter-bootstrap


    【解决方案1】:

    document.location 将返回对象,因此请尝试使用document.location.href,它将返回 URL 字符串。然后 AJAX 调用将开始工作。

    【讨论】:

      【解决方案2】:

      从您的成功函数中删除return。更改元素的 html 时不需要返回。仅在执行以下操作时才需要返回:

      function getHTML() 
      {
          $.ajax({
                  url: document.location,
                  type: "get",
                  sucess: function(data){
                      return data;
                  },
                  error: function(xhr){
                      return "Failed";
                  }
          });
      }
      
      var myHTML = getHTML();
      $('#body1').html(myHTML);
      

      但是,当您立即设置 html 时,您不需要它。

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 2021-12-23
        • 2018-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        相关资源
        最近更新 更多