繁体   English   中英

如何将当前网页的整个HTML文档发送到服务器?

[英]How can I send Current web page's whole HTML document to server?

如何将我当前看到的网页(意味着javascript处理的操作html documnet用于用户的视图 - 一种交互式AJAX网页)发送到服务器?

我可以将“所有html元素的documnet对象母亲”发送到服务器吗?

只需使用标准的js函数来获取'body'元素,它就是innerHTML

  var bodyHtml = document.getElementsByTagName('body')[0].innerHTML;

然后你可以使用ajax请求到服务器发送html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
     <style type="text/css">
         .contain-entire-page {
             display: none;
         }
     </style>
</head>
<body>
 <!-- us\sonawpa -->
    <form class="submit-entire-page" action="demo.php" method="post">
        <textarea class="contain-entire-page"></textarea>
    </form>
    <script type="text/javascript">

        $(document).ready(function () {
            var str = '<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml">';
            str += $('html').html();
            str += '</html>';
            $('.contain-entire-page').val(str);
            $('.submit-entire-page').submit();
        });
    </script>

</body>
</html>

如果要与网页内的Web服务器进行交互,可以提交表单(将Web请求发布到Web应用程序)或请求URL(从URL获取Web请求)。

如果要向服务器发送内容,可以POST所需的参数,当服务器获取请求时,它将执行一些功能并给出响应。 POST样本:

    POST www.xxxx.com?name=asdf HTTP/1.1
    Accept-Encoding: gzip,deflate
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    Content-Length: 97
    Host: www.xxxx.com
    Connection: Keep-Alive
    User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

回应是:

    HTTP/1.1 200 OK
    Content-Type: application/json
    Cache-Control: no-store
    Pragma: no-cache
    Date: Thu, 31 Oct 2013 08:04:29 GMT
    Transfer-Encoding: chunked
    Connection: Keep-Alive
    <html><body>Hello World</body></html>

希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM