gpt4 book ai didi

javascript - 从 AJAX 获取原始 HTTP 响应

转载 作者:行者123 更新时间:2023-11-30 20:27:57 24 4
gpt4 key购买 nike

在浏览器中使用纯 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>

(注意:我不是在谈论跨源请求)

最佳答案

您需要结合两个调用来获取正文和响应 header 。


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);
}
};

关于javascript - 从 AJAX 获取原始 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com