【问题标题】:unable to extract / read response herders using fetch无法使用 fetch 提取/读取响应牧民
【发布时间】:2020-08-16 19:25:13
【问题描述】:

我正在向node 服务器发送请求;服务器以标题和空白正文响应。我可以在开发工具的网络活动面板中看到标题,但无法读取它们。这是我的代码:

 
    let uploaded = await fetch(presignedUrl, {
                        method: 'PUT',
                        body: blob
                      }).then(response => {

                        console.log('test 1 -', response.headers);

                        response.headers.forEach(item => {
                          console.log('test 2 -', item);
                        });

                        for (var pair of response.headers.entries()) {
                          console.log('test 3 -', pair[0] + ': ' + pair[1]);
                        }

                        console.log('test 4 -', response.headers.get('ETag'));

                      });

这是它返回的内容: https://i.stack.imgur.com/TF1Lq.png

我已经尝试过 foreach 和 entries ,也获得了牧民类的功能。 问题可能是我正在制作一个 aws s3 put reuest (CORS) 上传是成功的,他们没有任何类型的请求错误,只是我无法读取响应 ETag 标头。

【问题讨论】:

  • 我不确定您所说的“无法阅读它们”是什么意思,因为根据您的代码,您甚至没有尝试阅读它们。您的输出看起来不错 - 正如预期的那样,它是一个 Headers 对象。检查docs 以查看可以调用哪些方法,例如获取某些标头(get)或枚举所有标头(forEachentries)。例如,试试console.log(response.headers.get('Content-Type'))console.log(Object.fromEntries(response.headers.entries()))
  • 这能回答你的问题吗? JS fetch, not getting headers on response
  • 我检查了文档我知道什么 response.headers.get('header-name');但它只是响应 null,我已经尝试过 for-each 和 entries。
  • 对我来说它有效(试试fetch('https://httpbin.org/status/200').then(response => { console.log(response.headers.get('Content-Type')) } );,你会得到text/html; charset=utf-8),我无法重现你的问题,因为http://example.com/process不是一个真实的URL,我没有知道您正在访问什么标头。请展示一个可重现的例子。
  • 您提到“我已经尝试过 forEach 和 entries” - 但您没有显示您如何尝试的代码,因此我们无法告诉您您做错了什么。请将其添加到您的问题中。

标签: javascript node.js angular fetch


【解决方案1】:

要访问除 cors 请求允许的之外的 aws 标头,您需要添加这些牧民以在您的 cors 配置中的公开标头中公开牧民。 (文档:https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 2020-07-21
    • 2017-09-06
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多