【问题标题】:show html page with jquery用 jquery 显示 html 页面
【发布时间】:2011-07-04 16:00:01
【问题描述】:

我有一个从 html 到 /html 的整个 html 页面,包括脚本标签、css 等,存储​​在数据库中。我正在使用 jquery 根据来自另一个网站的数据库中行的 id 从 php 脚本中获取变量。 这一切都完美无缺,但删除了 BODY 和 HEAD 标签 - 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; 
}
?>

【问题讨论】:

  • 我认为您可能错过了 AJAX 的重点...您为什么要替换整个 HTML 内容?为什么不直接更换需要更换的位?
  • 这个相关问题看起来可能对您有用:stackoverflow.com/questions/1236360/…
  • 您到底想达到什么目的?如果你想显示整个 HTML 页面,为什么不通过 URL 访问呢?
  • 感谢 Treborbob .. 有效。 maenu,我想只使用脚本标签在另一个 URL 上生成页面。
  • 它适用于除 IE 之外的所有浏览器.. sigh

标签: jquery html ajax


【解决方案1】:

为什么不直接使用&lt;form&gt; 并发布呢?简单多了,而且已经是非常基本的浏览器功能了:

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

当您发布表单并且服务器以 HTML 页面响应时,浏览器会将其加载到旧页面上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多