当前位置:网站首页>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
边栏推荐
- Tencent Cloud [Hiflow] New Era Automation Tool
- 沃谈小知识 |“远程透传”那点事儿
- Dameng 8 database export and import
- 用Unity发布APP到Hololens2无坑教程
- 今年七夕,「情蔬」比礼物更有爱
- Never put off till tomorrow what you can put - house lease management system based on the SSM
- QT language file production
- 【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can
- 队列题目:最近的请求次数
- Is your data safe in this hyperconnected world?
猜你喜欢
shell脚本:for循环与while循环
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
告白数字化转型时代,时速云镌刻价值新起点
[Software testing] unittest framework for automated testing
QT MV\MVC structure
Use SuperMap iDesktopX data migration tool to migrate map documents and symbols
Why did they choose to fall in love with AI?
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
【已解决】Unity Coroutinue 协程未有效执行的问题
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
随机推荐
[Solved] Unity Coroutine coroutine is not executed effectively
Solana NFT开发指南
结构体初解
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
YYGH-13-Customer Service Center
Bubble Sort and Quick Sort
Summary of domestic environments supported by SuperMap
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can
Call Alibaba Cloud oss and sms services
Growth-based checkerboard corner detection method
Kubernetes 网络入门
rpc-remote procedure call demo
Queue Topic: Recent Requests
达梦8数据库导出导入
Solve the problem of port occupancy Port xxxx was already in use
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕