axios跨域处理!

142 阅读1分钟
  1. 使用npm安装axios插件 npm install axios

  2. config目录下 index.js文件 proxyTable: {
    '/douban_api': {
    target: 'api.douban.com', //目标接口域名
    pathRewrite: {
    '^/douban_api': '' //重写接口
    },
    changeOrigin: true, //是否跨域
    },

    },

  3. main.js

     import  Axios from  'axios'
     Vue.prototype.$axios=Axios      
     Vue.prototype.HOST="/douban_api"
    
  4. components下 HelloWorld.vue组件

export default {

name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted() {
const url=this.HOST+"/v2/movie/top250";
this.$axios.get(url)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}

}
5.