当前位置:网站首页>跨域解决方案
跨域解决方案
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
边栏推荐
- 2022杭电多校第三场 L题 Two Permutations
- gorm joint table query - actual combat
- Inter-process communication and inter-thread communication
- 5.PCIe官方示例
- torch.autograd.grad finds the second derivative
- tiup status
- JUC线程池(一): FutureTask使用
- Pytorch使用和技巧
- 软件测试面试题:软件测试类型都有哪些?
- 2022 Nioke Multi-School Training Session H Question H Take the Elevator
猜你喜欢
随机推荐
Software testing interview questions: How many types of software are there?
ORA-00257
Getting Started with Kubernetes Networking
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
软件基础的理论
GCC: compile-time library path and runtime library path
僵尸进程和孤儿进程
GCC: paths to header and library files
tensor.nozero(),面具,面具
2022杭电多校 第三场 B题 Boss Rush
Software Testing Interview Questions: What do test cases usually include?
软件测试面试题:软件都有多少种分类?
软件测试面试题:软件验收测试的合格通过准则?
Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
PCIe Core Configuration
Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
[idea] idea configures sql formatting
If capturable=False, state_steps should not be CUDA tensors
工具类总结
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations

![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)






