vue-cli与后台数据交互增删改查

1. 安装vue-resource 

npm install vue-resource --save

2.访问后台地址,在vue中会出现跨域的问题,以下为解决方案

  在config下的index.js 中配置proxyTable代理设置

proxyTable: {
        '/api':{
            target:'http://19******:8080',
            changeOrigin:true,//允许跨域
            pathRewrite:{
                '/api':'/'
            }
        }
    },

3.地址配置完了,就可以开始访问后台,获取数据了
 我用的是vue-resource,有多种方式 get,post,json,jsonp,具体使用方法,可以查看官网

this.$http.get('url').then(response => { 
        console.log(response.body); 
      }, response => { 
        console.log(response); 
  });
this.$http.post(('url'),data ,{emulateJSON:false}).then(response => { 
       console.log(response.body);
      }, response => {
       console.log(response);
  });

 

猜你喜欢

转载自www.cnblogs.com/Kyaya/p/10154619.html