Javascript 获取当前页面的html代码,并使用post方法将其传递到变量中

Javascript 获取当前页面的html代码,并使用post方法将其传递到变量中,javascript,jquery,Javascript,Jquery,我试图用方法POST捕获并传递页面的当前HTML代码。我正在使用: var html_document = document.documentElement.innerHTML; document.body.innerHTML += '<form id="myform" action="myurl.php" method="post" target="_blank"><input type="hidden" name="html_document_name" value="

我试图用方法
POST
捕获并传递页面的当前HTML代码。我正在使用:

var html_document = document.documentElement.innerHTML;

document.body.innerHTML += '<form id="myform" action="myurl.php" method="post" target="_blank"><input type="hidden" name="html_document_name"  value="'+ html_document +'"></form>';
document.getElementById("myform").submit();
它工作正常。如果我尝试

alert(html_document);

它工作正常。

使用标记的jquery特定解决方案,请参阅下面的代码以捕获HTML

var html = $("html").html();

If you want to also capture the hmtl tags you could concatenate them to the html like this:
function getPageHTML() {
  return "<html>" + $("html").html() + "</html>";
}
var html=$(“html”).html();
如果您还想捕获hmtl标记,可以将它们连接到html,如下所示:
函数getPageHTML(){
返回“+$”(“html”).html()+”;
}

您的请求没有及时完成,因为您没有等待。使用alert时,它是一个阻塞调用,因此可以正常工作

使用jQueryAjax处理您的请求。并在成功函数中进行处理

 $.ajax(
    {
        url: someUrl,
        type: 'POST',
        success: function (data) { someSuccessFunction(data); },
        error: function (jqXHR, textStatus, errorThrown) { someErrorFunction(); }
    });

我收到:请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。
 $.ajax(
    {
        url: someUrl,
        type: 'POST',
        success: function (data) { someSuccessFunction(data); },
        error: function (jqXHR, textStatus, errorThrown) { someErrorFunction(); }
    });