vue axios跨域请求解决

vue axios跨域请求解决

1、修改vue项目下的config目录下的index.js

index.js

proxyTable: {
  "/api": {
    target: "http://192.168.0.111:8888/", //这里是修改你要访问的后台服务器的ip+端口
    changeOrigin: true,
    pathRewrite: {
      '^/api': ''  //这里理解成用‘/api’代替target里面的地址,
    }
  }  

2、写一个请求,这里访问http://192.168.0.111:8888/account/login,但是在axios里面写/api/account/login,这里的api就是代表了http://192.168.0.111:8888,有些人还没看懂。

 	this.$axios.post('/api/account/login',admin).then(res=>{
            var result=res.data;
            if(result.username==this.username){
                this.$router.push('/main');
            }
        })

3、看一下访问的结果,看URL写的是http://localhost:8080/api/account/login,看到这里大家不要大惊小怪,这里的http://localhost:8080/api就是代表我们访问后端的http://192.168.0.111:8888,但是如果单独去访问http://localhost:8080/api/account/login会报错404的。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值