proxyTable: { '/api': { target: 'http://xxxxxx.com', // 请求的接口的域名 // secure: false, // 若是是https接口,须要配置这个参数 changeOrigin: true, // 若是接口跨域,须要进行这个参数配置 pathRewrite: { '^/api': '' } } }, //当你发起请求时前面加上'/api/'就表明着'http://xxxxxx.com' // 注意: '/api' 为匹配项,target 为被请求的地址,由于在 ajax 的 url 中加了前缀 '/api',而本来的接口是没有这个前缀的,因此须要经过 pathRewrite 来重写地址,将前缀 '/api' 转为 '/'。若是自己的接口地址就有 '/api' 这种通用前缀,就能够把 pathRewrite 删掉。
1 export defalut{ 2 created(){ 3 fetch('地址',{ 4 method:'post',//请求的方式 5 //请求头 6 headers:{ 7 'Content-type':'application/json', 8 token:'', 9 }, 10 // body:'post请求的参数', 11 body:JSON.stringify({username:,password:}) 12 }) 13 .then(result=>{//请求成功的结果 14 console.log(result) 15 }) 16 } 17 }
1 import axios form 'axios' 2 //设置请求的headers 3 axios.defaults.headers.common['token']='' 4 axios.defaults.headers.post['Content-type']='application/json' 5 Vue.prototype.$axios=axios
1 export defalut{ 2 created(){ 3 this.$axios.post('地址',{ 4 //参数 5 }) 6 .then(data=>{ 7 console.log(data) 8 }) 9 } 10 }