fetch的mode值设置成cors报跨域错误

调用一个打印服务httpprinter

img

mode设置成no-cros时 不报错 但是返回的是null

img

mode 设置成cors 时 报错 跨域

img

排查过后台代码已经设置了可以跨域访问的配置 但是还是不行

我看了一下MDN,设置跨域需要加上

{
  credentials: 'include'
}

注意:mode: "no-cors" 仅允许使用一组有限的 HTTP 请求头:

  • Accept
    Accept-Language
    Content-Language
    Content-Type 允许使用的值为:application/x-www-form-urlencoded、multipart/form-data 或 text/plain

发送带凭据的请求

为了让浏览器发送包含凭据的请求(即使是跨域源),要将 credentials: 'include' 添加到传递给 fetch() 方法的 init 对象。

fetch('https://example.com', {
  credentials: 'include'
});