options跨域

This commit is contained in:
pplokijuhyg 2020-01-14 09:44:01 +08:00
parent 9bc59c1310
commit 76bcdbcf6e

10
app.js
View File

@ -10,6 +10,16 @@ reptile().then((res)=>{
reptilelist = res;
})
const app = new Koa();
app.use(async (ctx, next)=> {
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
if (ctx.method == 'OPTIONS') {
ctx.body = 200;
} else {
await next();
}
});
app.use(async (ctx, next) => {
// 允许来自所有域名请求
ctx.set("Access-Control-Allow-Origin", "*");