2011-07-04 133 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页面进行响应,浏览器加载它在旧的页面。