【发布时间】:2019-06-21 18:33:00
【问题描述】:
在 ReactJS 组件中,对重定向到另一个 API 的 API 的 fetch 调用不会返回最终目的地的响应。 Fetch 只返回 opaque 响应,返回 null 0 等,就好像您的呼叫失败一样。它甚至没有说它重定向。但在 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;
});
SocodedQueryURL 响应头:
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 响应标头:
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
【问题讨论】:
-
您从哪个 URL 重定向到哪个目标 URL?
-
这是一个内部 API,所以我无法分享,但您是否建议这里可能存在与服务器相关的问题,而不是前端
-
不,我建议你因为不同的域或其他东西而不允许看到响应
标签: javascript ajax reactjs fetch-api