当前位置:网站首页>cross domain solution
cross domain solution
2022-08-05 03:25:00 【shewlong】
使用proxy解决跨域问题
问题引入
If the front end does not passproxy设置代理,The request interface will report a cross-domain problem
Access to XMLHttpRequest at 'https://www.baidu.com/s?ie=utf+-+8&mod=1&isbd=1&isid=28B087E526051712&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=this.$axios&fenlei=256&oq=this.%2524axios&rsv_pq=af3009be00003a79&rsv_t=1c5fWtMk%2B6xwWKQqibPt6DyyGzLEaQSdASvH93Q6UDEXyZdfGTnRxojuNms&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&bs=this.$axios&_ss=1&clist=&hsug=&f4s=1&csor=' from origin 'http://172.16.20.231:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
解决方案
1.在vue.config.ts中进行配置
//vue-cli3.0 里面的 vue.config.js做配置
devServer: {
proxy: {
'/cdn': {
target: 'https://position.csdnimg.cn', // 后台接口域名
ws: true, //如果要代理 websockets,配置这个参数
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, //是否跨域
pathRewrite:{
'^/cdn': '/'
}
}
}
}
2.Send a request on a specific page
Suppose the backend address to be requested is ’https://position.csdnimg.cn/oapi/get’
//使用axios进行请求,首先在main.ts中将axios挂载到vue原型中
import axios from 'axios'
Vue.prototype.$service = axios
//used in specific pagesthis.$axios进行请求,Native is not packaged
this.$axios({
method: "get",
//上面的proxy会将url地址解析为https://position.csdnimg.cn/oapi/get
url: "/cdn/oapi/get",
params: {
// Here is the data sent to the background
},
}).then((response) => {
// 这里使用了ES6的语法
console.log(response); // 请求成功返回的数据
});
3.Address substitution may be required under certain conditions
var url = "https://position.csdnimg.cn/oapi/get"
var reg = /^http(s)?:\/\/(.*?)\//
// 必须是http开头或者https开头,结尾为'/'
// 把hostreplace with the specified value
var ToReplace = 'cdn/'
url.replace(reg, ToReplace)
// Host/cench
//*******************************************
If only need to take out eg:position.csdnimg.cn,in the following way
reg.exec(url)[2]
// www.cnblogs.com
边栏推荐
- Queue Topic: Recent Requests
- sql server installation prompts that the username does not exist
- 2022 Hangzhou Electric Multi-School 1st Game
- 【软件测试】自动化测试之unittest框架
- 龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
- The linear table lookup
- Open Source License Description LGPL
- Flink 1.15.1 Cluster Construction (StandaloneSession)
- Use @Mapper to query the partition status of oracle and report an error
- STM32 uses stm32cubemx LL library series tutorial
猜你喜欢

Simple description of linked list and simple implementation of code

CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
![[Software testing] unittest framework for automated testing](/img/80/caedd5cf6dd61c9d75475866613cac.png)
[Software testing] unittest framework for automated testing

论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕

龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入

ASP.NET application--Hello World

Flink 1.15.1 Cluster Construction (StandaloneSession)

Use SuperMap iDesktopX data migration tool to migrate ArcGIS data

2022-08-04 The sixth group, hidden from spring, study notes

QT language file production
随机推荐
基于生长的棋盘格角点检测方法
21 Days Learning Challenge (2) Use of Graphical Device Trees
ffmpeg 枚举decoders, encoders 分析
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
STM32 uses stm32cubemx LL library series tutorial
Walter talked little knowledge | "remote passthrough" that something
dmp (dump) dump file
Use Unity to publish APP to Hololens2 without pit tutorial
Solana NFT开发指南
【已解决】Unity Coroutinue 协程未有效执行的问题
Is your data safe in this hyperconnected world?
调用阿里云oss和sms服务
Solve the problem of port occupancy Port xxxx was already in use
Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
In 2022, you still can't "low code"?Data science can also play with Low-Code!
大佬们,我注意到mysql cdc connector有参数scan.incremental.sna
rpc-remote procedure call demo
This year's Qixi Festival, "love vegetables" are more loving than gifts
(11) Metaclass
mysql can't Execute, please solve it