如何从xmlhttprequest中获取html

how to get html from xmlhttprequest

本文关键字:获取 html xmlhttprequest      更新时间:2023-09-26

这对我来说很难
我尝试使用xmlhttprequest level 2。我试图从url中获取html并显示在div标记id=mydiv中。我创建123.php并在examplep中运行

  <script>
    function createCORSRequest() {
        var xhr = new XMLHttpRequest();
        if ("withCredentials" in xhr) {
            xhr.open("get", "http://www.ebay.com", true);
        } else if (typeof XDomainRequest != "undefined") {
            xhr = new XDomainRequest();
            xhr.open(method, url);
        } else {
            xhr = null;
        }
        return xhr;
    }
  </script>

和代码函数getpage()

    function getpage() {
    var request = createCORSRequest();
      if (request) {
        request.onload = function() {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        };
        request.send();
      }
    }

代码html

<button type="button" onclick="getpage()">Request data</button>
<div id="myDiv"></div>

我如何从中获取html代码。

以及如何添加头xmlhtmlrequest

Host: www.ebay.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: vi-vn,vi;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate

有人能帮我解决问题吗?

尝试更改此行:

    request.onload = function() {
        document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    };

收件人:

    request.onload = function() {
        document.getElementById("myDiv").innerHTML = this.responseText;
    };