300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Vue2.0使用Axios实现跨域访问(解决跨域问题)

Vue2.0使用Axios实现跨域访问(解决跨域问题)

时间:2023-06-03 20:56:14

相关推荐

Vue2.0使用Axios实现跨域访问(解决跨域问题)

Vue2.0使用Axios实现跨域访问

配置BaseUrl

在main.js中,配置下我们访问的Url前缀:

axios.defaults.baseURL = '/api' // 开发本地代理axios.defaults.headers.post['Contenst-Type'] = 'application/json;'axios.mon['token'] = store.state.token

配置代理

修改config文件夹下的index.js文件,在proxyTable中加上如下代码:

proxyTable: {'/api': { //代理地址target: 'http://xxx.xxx.xxx.xxx:8088', //需要代理的地址, 实际生产环境需要访问的地址changeOrigin: true, //是否跨域secure: false,pathRewrite: {'^/api': '/' //本身的接口地址没有 '/api' 这种通用前缀,所以要rewrite,如果本身有则去掉}}},

修改请求Url

this.$axios.get("/test/test123") .then(res=>{console.log(res)}).catch(err=>{console.log(err)})

给url加上了前缀/api,我们访问"/test/test123"就当于访问了:localhost:8080/api/test/test123

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。