1: 所谓服务器中编写代码就是中间件中的代码
中间间的本质: 就是函数
2: 允许跨域
2.1: 实际是通过Ajax 访问服务器
2.2: 同源策略
同源策略就是: 同协议/ 同域名/ 同端口
当前页面的地址和Ajax 获取的地址
2.3: 设置响应头:
app.use(async (ctx, next)=> {
ctx.set("Access-Control-Allow-Origin", "*");
ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE");
await next();
})
// 响应头的中间件
module.exports = async (ctx, next)=> {
const contentType = 'application/json; chartset=utf-8'
ctx.set('Content-Type', contentType)
ctx.set("Access-Control-Allow-Origin", "*");
ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE");
// ctx.response.body = '{"success": true}'
await next()
}
Koa 中设置允许跨域
最新推荐文章于 2023-11-29 11:52:10 发布