如何评估fetch api中收到的http响应 [英] how to evaluate http response received in fetch api

查看:119
本文介绍了如何评估fetch api中收到的http响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何评估在fetch api中检索到的http响应(在同一个源上制作XMLHttpRequest时,有没有办法不发送cookie?

How can we evaluate the http response retrieved in fetch api (Is there a way to not send cookies when making an XMLHttpRequest on the same origin?)?

我们可以通过提取调用访问responseText。现在我们可以以编程方式(可能通过对responseText的api调用)执行浏览器所做的事情(称为页面加载),例如下载图像,css文件等,并评估脚本以及可能的xmlhttprequests(如果有的话)。

we get access to the responseText as a result of the fetch call. Now can we do programmatically (possibly via an api call on responseText) things the browser does (called page loading) like downloading the images, css files etc and evaluating the scripts and possibly making xmlhttprequests if there are any.

我希望在完成所有这些操作后获取页面的html内容,即相当于浏览器从加载状态变为完成状态后完成所有这些。

I want to get to the html content of the page when all this is done, i.e., equivalent to when the browser goes from "loading" state to "complete" after finishing all this.

我希望问题很清楚。

推荐答案

您可以将 response.text()呈现为< iframe> (带可见性:隐藏如果您愿意的话)。然后,您可以使用所有标准函数操作新文档:

You could render the response.text() into an <iframe> (with visibility: hidden if you wish). Then you can manipulate the new doc with all the standard functions:

<!DOCTYPE html>
<html>

<head>
  <title>This is the page title</title>
  <meta charset="UTF-8">
  <meta name="description" content="Free Web Help">
  <meta name="keywords" content="HTML,CSS,XML,JavaScript">
  <meta charset="utf-8">
</head>

<body>

</body>

<script>
  var img = new Image();
  img.src = "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png";
  document.body.appendChild(img);
  fetch("so.html")
    .then(function(response) {
      return (response.text());
    })
    .then(function(responseText) {
      // limit the recursion depth to 1
      if (!window.frameElement) {
        var newDoc = document.createElement('iframe');
        document.body.appendChild(newDoc);
        newDoc.contentDocument.write(responseText);
      }
    });
</script>

</html>

要自己试试,可以将此代码保存为 so.html 并在本地Web服务器上访问它,或者您可以在这里查看

To try this yourself, you can save this code as so.html and access it on your local web server, or you can check it out here.

这篇关于如何评估fetch api中收到的http响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆