当前位置:网站首页>CROS和JSONP配置
CROS和JSONP配置
2022-08-05 08:06: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'
}
//默认origin只支持一个域名或者*表示全部,如果想支持具体的多个指定域名可以如下设置:
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' //默认允许本地请求可跨域
},
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};
//4.使用:
//router.js文件
router.get('/cors',controller.home.cors)
//home.js文件
async cors(){
this.ctx.body={ info:"这个接口可以通过跨域访问"}
}
5. 注意:跨域前端请求时需要携带跨域凭证,不然缓存会失效
前端网络请求时配置:
原生ajax:
xhr.withCredentials = true;
axios:
axios.post(url,{},{ withCredentials:true}).then((res)=>{})
后端配置:
config.cors = {
origin: '具体的ip和端口 不能写* ',
credentials:true
}
2.实现jsonp接口
//如果前端的参数中有cb=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);
};
也可以直接在jsonp方法中直接配置,功能一样:
//如果前端的参数中有cb=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);
};
边栏推荐
- TensorFlow安装步骤
- [NOIP2010 提高组] 机器翻译
- Fiddler tool explanation
- uniapp时间组件封装年-月-日-时-分-秒
- 本地能ping通虚拟机,虚拟机ping不通本地
- v-if/v-else determines whether to display according to the calculation
- 常用的遍历map的方法
- Antdesign a-select 下拉框超出长度换行显示
- SQL SERVER on master-slave table trigger design
- Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
猜你喜欢

Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation

Redis implements distributed lock-principle-detailed explanation of the problem

Chapter3、色调映射

Fiddler tool explanation

Jmeter永久设置中文界面

VXE-Table融合多语言

七夕看什么电影好?爬取电影评分并存入csv文件

双向循环带头链表

图扑软件与华为云共同构建新型智慧工厂

U++ UE4官方文档课后作业
随机推荐
标准C语言15
RedisTemplate: error template not initialized; call afterPropertiesSet() before using it
图片地址转为base64
Ethernet Principle
【结构体内功修炼】结构体实现位段(二)
[Structure internal power practice] Structure memory alignment (1)
线程池的创建及参数设置详解
uniapp时间组件封装年-月-日-时-分-秒
unity urp 渲染管线顶点偏移的实现
每一个女孩曾经都是一个没有泪的天使
[Repost] Marry a man must marry a man whose salary is at least 3571.4 yuan higher than yours
Discourse 清理存储空间的方法
TRACE32——外设寄存器查看与修改
openSource 知:社区贡献
Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
A small problem with mysql using the in function
别把你的天使弄丢了
U++ UE4官方文档课后作业
Redis常用命令