2017-07-03 72 views
0

我目前正試圖在我的本地主機做一個簡單的AJAX頁面轉換。AJAX頁面加載返回[對象對象]

這裏是我的home.html的

<a href="/blank.html">go to blank</a> 

應該帶我到包含blank.html頁面的標籤。

這裏是我的AJAX:

$(function(){ 
    jQuery(function ($) { 
     $(document).on('click', "a", function (event) { 
      event.preventDefault(); 
      $.when($("body").fadeOut(1000).promise(), $.ajax({ 
       url: this.href, 
       type: 'get', 
       dataType: 'html' 
      })).done(function (html) { 
       var newDoc = document.open("text/html", "replace"); 
       newDoc.write(html); 
       newDoc.close(); 
       $("body").fadeIn(1000); 
      }); 
     }); 
    }); 
}); 

沒有我的重定向工作正常的JavaScript,但使用JavaScript,在淡出後,頁面上的返回[object Object](和不重定向)

我有四處尋找解決方案,但似乎無法找到一個解決方案。

任何幫助或建議表示讚賞,謝謝你提前!

編輯:

console.log(html);返回:

enter image description here

+0

是什麼'的console.log(HTML)'的結果呢? –

+0

請參閱編輯! @MaheshSinghChouhan –

+0

'blank.html'裏面是什麼?你的Ajax請求試圖獲得正常的HTML內容作爲'dataType:'''''但不知何故反應是拋出一個對象,這就是問題 –

回答

1

試試這個:

$(function(){ 
    jQuery(function ($) { 
     $(document).on('click', "a", function (event) { 
      event.preventDefault(); 
      $("body").fadeOut(1000); 
      $.ajax({ 
       url: this.href, 
       type: 'get', 
       dataType: 'html', 
       success: function(html){ 
       var newDoc = document.open("text/html", "replace"); 
        newDoc.write(html); 
        newDoc.close(); 
        $("body").fadeIn(1000); 
       } 
      }) 

     }); 
    }); 
});