fetch 的响应为空,即使请求成功

fetch's response is empty even though the request was successful

我正在尝试使用 fetch 函数在客户端 Javascript 代码中从服务器检索数据。我在 Chrome 中使用名为 whatwg-fetch 的 fetch polyfill 版本(内部支持 fetch)。

我是这样调用函数的:

//Called in a page loaded as http://localhost:3236/
fetch("http://localhost:8080/list", {
         mode: 'no-cors',
         credentials: "include", 
    })
    .then(function(response) {
        return response.json();
    });

如您所见,我的应用程序服务器与我的资源服务器不同,因此我必须为 CORS 设置选项。问题是 response 没有内容:

{
    body: null,
    bodyUsed: false,
    headers: Headers,
    ok: false,
    status: 0,
    statusText: "",
    type: "opaque",
    url: "",
}

在这种情况下,当我转到网络面板并发现已发送请求时,状态为 200 和请求的数据似乎一切正常。

我怎样才能找到问题所在?

线索在opaque键中。请求成功,但 Chrome 的 CORS 限制阻止您使用返回的数据。 no-cors 选项可疑;如果您的服务器已针对跨源请求正确配置,则没有必要。

N.B。如果您已正确配置所有内容,但在本地测试客户端时遇到错误,则使用 --disable-web-security 标志启动 Chrome 可能就足够了。