鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 跨域請(qǐng)求服務(wù)器中的css文件,css請(qǐng)求成功,但是字體文件請(qǐng)求失敗

跨域請(qǐng)求服務(wù)器中的css文件,css請(qǐng)求成功,但是字體文件請(qǐng)求失敗

在開發(fā)者工具network中能夠看到字體文件的請(qǐng)求返回的是成功,但是console中輸出了以下內(nèi)容

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.

這是在服務(wù)中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'],
})
回答
編輯回答
心夠野

找到問題了,在koa的配置里,我把托管靜態(tài)文件的中間件放到了跨域配置的中間件前面,導(dǎo)致在請(qǐng)求靜態(tài)文件時(shí),服務(wù)器沒有處理靜態(tài)文件的跨域問題。

現(xiàn)將跨域配置的中間件的順序提前,解決了問題。

2018年6月4日 00:34