vue.js处理跨域问题

第一步: 修改 config--->index.js
proxyTable: {
  "/api":{
    target:"http://localhost:3000",
    changeOrigin:true,
    pathRewrite:{
      '^/api':''
    }

第二步:min.js添加HOST:

Vue.prototype.HOST='/api'

第三步:组件内的请求写法:

created(){
    var url = this.HOST+"/movie/top250";
    this.$axios.get(url,{
        params:{
        count:10,
        start:0
        }    
    })
    .then(res => {
        this.filterData(res.data);
    })
    .catch(error =>{
        console.log(error)
    })
}

第四步:配置文件修改后要重启vue.js

  //此种方式只适用于测试阶段,打包的时候不具备服务器,不能跨域了,后端解决

//注:要把main.js 中的全局请求地址注释掉。

猜你喜欢

转载自blog.csdn.net/qq_25635139/article/details/87607666