Vue2.0 axios 前后端分离跨域问题 配置axios或代理(两行代码解决)
安装axios
npm install axios
代理方式
项目根目录下创建vue.config.js
module.exports = {
devServer: {
proxy: 'http://localhost:9000'
}
}
测试代码
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<br>
{{msg}}
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
data:()=>{
return {
msg:'123'
}
},
async mounted() {
console.log('发起请求')
let res = await this.$axios.get('/examination/list?page=1&limit=10')
console.log(res)
this.msg = res.data
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
配置axios baseURL方式
axios.defaults.timeout=30000
axios.defaults.baseURL='http://localhost:9000'
两种方式的区别是:
代理方式的话不会显示后端地址,仍然是走的当前IP、端口,而baseURL方式会直接显示走的配置的地址,建议使用代理配置方式。
本文来自博客园,作者:HumorChen99,转载请注明原文链接:https://www.cnblogs.com/HumorChen/p/18039602
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~