Fetch 未从 302 重定向调用中检索响应

Fetch is not retrieving the response from the 302 redirected call

在 ReactJS 组件中,对重定向到另一个 API 的 API 的提取调用不会 return 最终目的地的响应。只需使用空 0 等获取 returns opaque 响应,就好像您的呼叫失败一样。它甚至没有说它重定向了。但是在 Chrome 的控制台中,网络选项卡清楚地显示重定向调用成功。

            let call = fetch(encodedQueryUrl, {
                method: 'GET',
                cache: 'no-cache',
                mode: 'no-cors',
                redirect: 'follow',
                credentials: 'same-origin'
            }).then((response) => {
                console.log("Response???", response);
                return response;
            });

所以encodedQueryURL响应Headers:

Request Method: GET
Status Code: 302 
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: access-control-allow-origin
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin: *
content-length: 0
content-type: text/html; charset=UTF-8

以及 302 响应 headers:

Request Method: GET
Status Code: 200 
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: access-control-allow-origin
access-control-allow-methods: GET, POST, GET, OPTIONS, PUT, DELETE
access-control-allow-origin: *
content-type: application/json; charset=UTF-8

因为您使用的是模式:no-cors,所以您获得了不透明的响应。这是作为一种安全机制发生的,因为您正在避免 CORS 验证。所以,为了安全起见,响应的内容不能从 JS 代码访问,它只能用于缓存目的。

更新:据我了解,您使用 no-cors 可能是因为您遇到了 CORS 问题,正如您所说,因为您正在使用另一个域的资源,该域不同于 API.

为此,您需要:

  • 修改您的 API 以将您的前端应用所在的域 运行 添加到已批准的来源列表中,以便您能够使用 API 或
  • 将您的前端应用程序 运行 与您的 API
  • 在同一台服务器上