vue中使用axios进行ajax请求数据

npm安装axios

npm install axios --save

引入axios

import axios from 'axios'

使用axios

mounted () {
  this.getHomeInfo()
},
methods: {
  getHomeInfo () {
  	 //跨域请求可以在config/index.js中进行配置
     axios.get('/zt/api/app_dujia/index.php')
       .then(this.getHomeInfoSucc)
  },
  getHomeInfoSucc (res) {
     console.log(res)
  }
}

开发环境配置请求接口config/index.js

配置多个跨域接口

proxyTable: {
 '/api' : {
    target: 'https://touch.dujia.qunar.com',
    changeOrigin: true,
    pathRewrite: {
      '^/api': ''
    }
 },
 '/zt' : {
    target: 'https://zt.dujia.qunar.com',
    changeOrigin: true,
    pathRewrite: {
      '^/zt': ''
    }
 }
},

参考链接:【VUE】一个简单常用的proxyTable配置
参考链接:webpack中代理配置(proxyTable)
使用webpack+axios修改ProxyTable错误之504
axios与jsonp

猜你喜欢

转载自blog.csdn.net/weixin_43756060/article/details/86479904