2019-01-10 axios异步跨域

如何实现axios异步跨域请求
在config/index.js文件中设置:

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

然后在main.js里面设置

import axios from 'axios'
Vue.prototype.$axios= axios
Vue.prototype.HOME ='/api'

然后在组件里面引入

  created() {
    var url = this.HOME;
    this.$axios.get(url)
      .then(res=>{
        console.log(res.data);
        this.scope=res.data.data.scope;
        console.log(this.scope)
      }).catch(error=>{
        console.log(error);
      });
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容