2011-07-04 251 views
1

我有一個從html到/ html的完整html頁面,包括存儲在數據庫中的腳本標記,css等。我正在使用jquery從基於數據庫中的行的ID從另一個網站的PHP腳本獲取變量。 這一切都很完美,但BODY和HEAD標籤被刪除 - 其餘的html完好無損。任何人都可以對此有所瞭解嗎? 這裏是我的代碼:用jquery顯示html頁面

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    function getListing(postid){ 
      $.ajax({ 
       type: "POST", 
       url: "http://my-website.com/url.php", 
       data: postid, 
       dataType: "html", 
       cache: false, 
       success: function(html) { 
        $("html").html(html); 
       } 
      }); 
     } 
    getListing("postid=6943"); 
}); 
</script> 

和PHP是非常基本的:

<?php header("Access-Control-Allow-Origin: *");?> 
<? if($_POST['postid']) { 
$postid = $_POST['postid']; 
// get html into a variable here *(code romved)* 
echo $html; 
} 
?> 
+8

我想你可能錯過了AJAX的觀點......爲什麼要替換整個HTML內容?爲什麼不只是更換需要更換的位? – lonesomeday

+0

這個相關的問題看起來像它可能對你有用:http://stackoverflow.com/questions/1236360/replace-the-entire-html-node-using-jquery – Treborbob

+0

你到底想要達到什麼目的?如果你想顯示整個HTML頁面,爲什麼不通過URL訪問它? –

回答

1

爲什麼不直接使用<form>和後呢?簡單了很多,這是一個非常基本的瀏覽器功能已經:

$(function() { 
    $('body').append($('<form/>', { 
    id: 'newForm', 
    action: "http://my-website.com/url.php", 
    method: "POST" 
    }).append($('<input/>', { name: 'postid' value: '6943' }))); 
    $('#newForm').submit(); 
}); 

在您發表形式和服務器的HTML頁面進行響應,瀏覽器加載它在舊的頁面。