代码之家  ›  专栏  ›  技术社区  ›  Juicy

从ajax获取原始http响应

  •  1
  • Juicy  · 技术社区  · 7 年前

    在浏览器中使用普通javascript ajax,我可以得到 未经加工的 来自服务器的http响应?

    我的意思是标题和正文是原始文本,比如:

    < HTTP/1.1 301 Moved Permanently
    < Location: http://www.google.co.uk/
    < Content-Type: text/html; charset=UTF-8
    < Server: gws
    < Content-Length: 221
    < X-XSS-Protection: 1; mode=block
    < X-Frame-Options: SAMEORIGIN
    < Age: 11
    < Date: Mon, 04 Jun 2018 09:12:14 GMT
    < Expires: Wed, 04 Jul 2018 09:12:14 GMT
    < Cache-Control: public, max-age=2592000
    < Connection: keep-alive
    < 
    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>301 Moved</TITLE></HEAD><BODY>
    <H1>301 Moved</H1>
    The document has moved
    <A HREF="http://www.google.co.uk/">here</A>.
    </BODY></HTML>
    

    (注:我不是说跨来源请求)

    1 回复  |  直到 7 年前
        1
  •  1
  •   Aliaksei Zhukau    7 年前

    您将需要组合两个调用以获取主体和响应头。


    var request = new XMLHttpRequest();
    request.open("GET", "http://www.example.com", true);
    request.send(null);
    request.onreadystatechange = function() {
      if (request.readyState == 4){
        console.log(request.getAllResponseHeaders());
        console.log(request.responseText);
      }
    };