繁体   English   中英

axios response.data 始终为空,但 Postman 有效

[英]axios response.data always empty but Postman works

我正在尝试做一个扩展,打开一个有点 URL 并获取标题:

http://bit[dot]ly/30YbIyD

Postman 遵循重定向并为我获取结果文档。

当我尝试使用 Axios 时,尽管似乎发生了重定向,但我得到了空的响应数据。

第一个请求收到指向“stackoverflow.com”的 301。 在那个请求中,我们得到“200 SUCCESS”但是数据部分是空的。

axios({
    'url': 'http://bit[dot]ly/30YbIyD',
    method: 'get',
    maxRedirects: 5,
    headers: {
      'Content-Type': 'text/html',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET',
    },
    withCredentials: true,
  })
  .then((res) => {
    console.log("Response", res)
  })
  .catch((err) => {
    console.log("Error with fetch: ", err)
  });

没有错误消息,但我希望看到与 Postman 返回相同的结果; 最终文件。

我还在浏览器控制台中收到这些警告:

Cross-Origin Read Blocking (CORB) blocked cross-origin response <URL> with MIME type text/html. See <URL> for more details.

与位于http://bit.ly/的跨站点资源关联的 cookie 设置为没有SameSite属性。 如果将跨站点请求设置为SameSite=NoneSecure ,Chrome 的未来版本将仅提供 cookies 。 You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032 .

以下是 Postman 发送的标头: https://imgur.com/765Cz30

使用以下步骤:
1. 使用 Axios 调用http://bit[dot]ly/ 30YbIyD。
2. 从响应 Header 中找到位置。 通过 Axios 访问响应 header
3.调用Location中的URL获取实际内容。

*** 浏览器和 Postman 正在执行多个请求以获取内容。 我在我的 Java 应用程序中使用了上述步骤。 以下代码供参考

if ( HttpStatus.SC_MOVED_TEMPORARILY  == statusCode ) {
            location = method.getResponseHeader("Location").getValue();
            if (sLog.isDebugEnabled()) {
                sLog.debug("location: " + location);
            }                                                                  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM