当前位置:网站首页>跨域解决方案
跨域解决方案
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
边栏推荐
- 4. PCIe interface timing
- Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
- GCC:屏蔽动态库之间的依赖
- EL定时刷新页面中的皕杰报表实例
- 2022杭电多校训练第三场 1009 Package Delivery
- Software Testing Interview Questions: What do test cases usually include?
- 软件测试面试题:软件都有多少种分类?
- Software testing interview questions: What are the seven-layer network protocols?
- Theory of Software Fundamentals
- Software testing interview questions: What is the difference between load testing, capacity testing, and strength testing?
猜你喜欢

JVM类加载简介

CNI(Container Network Plugin)

Inter-process communication and inter-thread communication

DHCP的工作过程

Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack

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

阶段性测试完成后,你进行缺陷分析了么?
![[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy](/img/33/3177b4c3de34d4920d741fed7526ee.png)
[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy

Introduction to JVM class loading

深度学习训练前快速批量修改数据集中的图片名
随机推荐
如何用 Solidity 创建一个“Hello World”智能合约
gorm joint table query - actual combat
Software Testing Interview Questions: About Automated Testing Tools?
自定义线程池
Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
Software testing interview questions: Please draw the seven-layer network structure diagram of OSI and the four-layer structure diagram of TCP/IP?
SV class virtual method of polymorphism
5.PCIe官方示例
僵尸进程和孤儿进程
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
4. PCIe 接口时序
2022杭电多校第一场 1004 Ball
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
EL定时刷新页面中的皕杰报表实例
Software Testing Interview Questions: What's the Key to a Good Test Plan?
MongoDB搭建及基础操作
Software Testing Interview Questions: Qualifying Criteria for Software Acceptance Testing?
2022杭电多校第三场 L题 Two Permutations