Koa2配置跨域

zhuanbike 2022-7-13 619

设置响应头

ctx.set('Acsess-Control-Allow-Origin','*')
ctx.set('Acsess-Control-Allow-Methods','OPTIONS,GET,PUT,POST,DELETE')

完整代码:

module.exports = async (ctx, next) => {
    const contentType = 'application/json;charset=utf-8'
    ctx.set('Content-Type', contentType)
    ctx.set('Acsess-Control-Allow-Origin','*')
    ctx.set('Acsess-Control-Allow-Methods','OPTIONS,GET,PUT,POST,DELETE')
    await next()
}
最新回复 (0)
发新帖