Blogger Information
Blog 37
fans 0
comment 1
visits 43064
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
在Vue中如何使用axios跨域访问数据
戈壁骆驼的博客
Original
3583 people have browsed it

vue.js学习之 跨域请求***与axios传参

一:跨域请求***

1:打开config/index.js

module.exports{
    dev: {
    }
}


在这里面找到proxyTable{},改为这样:

proxyTable: {
      '/api': {
        target: 'http://121.41.130.58:9090',//设置你调用的接口域名和端口号 别忘了加http
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''//这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可
        }
      }
    }

2:在需要调接口的组件中这样使用:

axios.post('/api/yt_api/login/doLogin',postData)
    .then(function (response) {
        console.log(1)
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    })

   


  注意:原接口:http://http://121.41.130.58:9090/yt_api/login/doLogin

      页面调用:http://localhost:8081/api/yt_api/login/doLogin ——这里多了一个/api/不是多余的,不要删

 


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
MacJayLee 2019-05-07 16:19:01
你这种方法是能在调试环境中生效 因为是写在dev中 打包部署后就凉了
1 floor
Author's latest blog post