当前位置:网站首页>CROS and JSONP configuration
CROS and JSONP configuration
2022-08-05 08:10:00 【cjx177187】
跨域
1.egg-cors框架提供了 egg-cors 插件来实现cors跨域请求.
//1.下载
cnpm i --save egg-cors
//2.开启插件
// config/plugin.js文件
cors:{
enable: true,
package: 'egg-cors',
}
//3.配置插件
// config/config.default.js文件
config.cors = {
origin: '*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
}
//默认originOnly support a domain name or*表示全部,If you want to support specific multiple specified domain name can be set as follows:
config.cors = {
// origin: ['http://localhost'],
origin:function(ctx) { //设置允许来自指定域名请求
console.log(ctx);
const whiteList = ['http://www.baidu.com','http://www.hqyj.com'];
let url = ctx.request.header.origin;
if(whiteList.includes(url)){
return url;
}
return 'http://localhost' //Default allows local requests a cross-domain
},
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};
//4.使用:
//router.js文件
router.get('/cors',controller.home.cors)
//home.js文件
async cors(){
this.ctx.body={ info:"This interface can be accessed through cross-domain"}
}
5. 注意:Need to carry proof of cross-domain cross-domain front-end requests,Otherwise the cache will failure
The front-end web request configuration:
原生ajax:
xhr.withCredentials = true;
axios:
axios.post(url,{},{ withCredentials:true}).then((res)=>{})
后端配置:
config.cors = {
origin: '具体的ip和端口 不能写* ',
credentials:true
}
2.实现jsonp接口
//If the parameters of the front end ofcb=fn参数(jsonp接口参数),将会返回JSONP格式的数据,否则返回JSON格式的数据.
//1.配置:
// config/config.default.js文件
config.jsonp = {
callback: 'cb', // 识别 query 中的 `cb` 参数
limit: 100, // 函数名最长为 100 个字符
};
//2.写接口
// app/router.js
module.exports = app => {
const jsonp = app.jsonp();
app.router.get('/api/posts', jsonp, app.controller.posts.list);
};
也可以直接在jsonpMethods direct configuration,功能一样:
//If the parameters of the front end ofcb=fn参数(jsonp接口参数),将会返回JSONP格式的数据,否则返回JSON格式的数据.
// app/router.js文件
module.exports = app => {
const jsonp = app.jsonp({
callback: 'cb',
limit: 100,
});
app.router.get('/api/posts', jsonp, app.controller.posts.list);
};
边栏推荐
猜你喜欢

Chapter 12 贝叶斯网络

SVG Star Wars Style Toggle Toggle Button

Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions

php向mysql写入数据失败

爬虫之验证码
![[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)](/img/39/d20f45ccc86ebc4e5aebc8e4d0115f.png)
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)

力扣刷题八月第一天

餐饮大单品「真香」,却没有穿透周期的能力
![[Structure internal power practice] Structure memory alignment (1)](/img/31/4ddc16810da8238ac95a93d007e12e.png)
[Structure internal power practice] Structure memory alignment (1)

宝塔实测-搭建中小型民宿酒店管理源码
随机推荐
路由----router
TensorFlow installation steps
Redis implements distributed lock-principle-detailed explanation of the problem
YOLOv3 SPP理论详解(包括CIoU及Focal loss)
生命的颜色占卜
Version number naming convention
Vulnhub target drone: HA_ NARAK
强网杯2022 pwn 赛题解析——house_of_cat
Chapter3、色调映射
漂亮MM和普通MM的区别
关于MP3文件中找不到TAG标签的问题
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
Constellation ideal lover
v-if/v-else根据计算判断是否显示
Stored procedure writing experience and optimization measures
Data source object management Druid and c3p0
星座理想情人
七夕看什么电影好?爬取电影评分并存入csv文件
青苹果论坛重新开放
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)