跨域请求服务器中的css文件,css请求成功,但是字体文件请求失败

在开发者工具network中能够看到字体文件的请求返回的是成功,但是console中输出了以下内容

Font from origin 'http://127.0.0.1:3000' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

这是在服务中koa2-cors的配置

app.use(cors({
  origin: function (ctx) {
      return '*'
  },
  exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
  maxAge: 5,
  credentials: true,
  allowMethods: ['GET', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
})
阅读 3.1k
1 个回答

找到问题了,在koa的配置里,我把托管静态文件的中间件放到了跨域配置的中间件前面,导致在请求静态文件时,服务器没有处理静态文件的跨域问题。

现将跨域配置的中间件的顺序提前,解决了问题。

推荐问题