从当前页面上的响应中重新加载完整的html代码

时间:2018-12-06 04:13:08

标签: javascript ajax

当我使用render_template从python获取完整的html代码时,

我想在当前页面上重新加载完整的html代码。

我知道我可以使用form方法,并且我知道可以使用$('#A').html(response)

  1. form方法:我不使用提交按钮,所以这不是我想要的。

  2. $('#A').html(response):这将从id A处添加代码。使用此代码时,我的完整html代码在当前页面内重复,而不是包装

因此,我需要如何使用已响应的完整html代码重新加载当前页面

我也尝试过

$.post('/cccc',{user_id: id}, function(response){
        window.location.replace(html(response));
    }
);

但这也会发生html not defined错误。

有人可以给我一些帮助吗?

1 个答案:

答案 0 :(得分:-1)

您需要使用document.write()

$.post('/cccc',{user_id: id}, function(response){
    document.open();
    document.write(response);
    document.close();
});

演示

<html>
    <script>
	    function replace(){
            const newhtml = '<html><body style="background-color: white"><p>New Content</p></body></html>';
            document.open()
            document.write(newhtml);
            document.close();
        }
    </script>
    <body>
        <p>Old Content</p>
        <a href='javascript:replace()'>Replace</a>
    </body>
</html>

JsFiddle Demo