当前位置:网站首页>跨域解决方案
跨域解决方案
2022-08-05 00:49:00 【shewlong】
使用proxy解决跨域问题
问题引入
前端如果不通过proxy设置代理,则请求接口会报跨域问题
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.在具体页面发送请求
假设要请求的后端地址为’https://position.csdnimg.cn/oapi/get’
//使用axios进行请求,首先在main.ts中将axios挂载到vue原型中
import axios from 'axios'
Vue.prototype.$service = axios
//在具体页面中使用this.$axios进行请求,原生未进行封装
this.$axios({
method: "get",
//上面的proxy会将url地址解析为https://position.csdnimg.cn/oapi/get
url: "/cdn/oapi/get",
params: {
// 这里是发送给后台的数据
},
}).then((response) => {
// 这里使用了ES6的语法
console.log(response); // 请求成功返回的数据
});
3.某些条件下可能需要对地址进行替换
var url = "https://position.csdnimg.cn/oapi/get"
var reg = /^http(s)?:\/\/(.*?)\//
// 必须是http开头或者https开头,结尾为'/'
// 把host替换成指定数值
var ToReplace = 'cdn/'
url.replace(reg, ToReplace)
// Host/cench
//*******************************************
如果只需要取出例如:position.csdnimg.cn,则通过以下方式
reg.exec(url)[2]
// www.cnblogs.com
边栏推荐
- Pytorch使用和技巧
- MongoDB搭建及基础操作
- Software testing interview questions: What are the strategies for system testing?
- torch.autograd.grad finds the second derivative
- 软件测试面试题:软件测试类型都有哪些?
- The principle of NMS and its code realization
- 2022杭电多校第三场 K题 Taxi
- Introduction to JVM class loading
- ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
- 2022牛客多校训练第二场 H题 Take the Elevator
猜你喜欢

进程间通信和线程间通信

5.PCIe官方示例

面试汇总:为何大厂面试官总问 Framework 的底层原理?

数仓4.0(三)------数据仓库系统

Will domestic websites use Hong Kong servers be blocked?

仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!

5. PCIe official example

matlab 采用描点法进行数据模拟和仿真

LiveVideoStackCon 2022 Shanghai Station opens tomorrow!

深度学习:使用nanodet训练自己制作的数据集并测试模型,通俗易懂,适合小白
随机推荐
2022牛客多校第三场 A Ancestor
node uses redis
Software testing interview questions: What are the seven-layer network protocols?
Activity Recommendation | Kuaishou StreamLake Brand Launch Conference, witness together on August 10!
Introduction to JVM class loading
2022杭电多校训练第三场 1009 Package Delivery
Software testing interview questions: How many types of software are there?
2021年11月网络规划设计师上午题知识点(下)
2022杭电多校 第三场 B题 Boss Rush
Software Testing Interview Questions: What Are the Types of Software Testing?
2022 Nioke Multi-School Training Session H Question H Take the Elevator
如何用 Solidity 创建一个“Hello World”智能合约
torch.autograd.grad求二阶导数
3. pcie.v file
2022多校第二场 K题 Link with Bracket Sequence I
canvas Gaussian blur effect
Software Testing Interview Questions: What do test cases usually include?
ORA-01105 ORA-03175
Software testing interview questions: What are the strategies for system testing?
GCC:编译时库路径和运行时库路径