npm install axios
在main.js中引入axios
import axios from 'axios'
Vue.prototype.$http = axios
这样在其他的vue组件中就可以this.$http 使用了 这个时候的/api是代理的名称
例如
this.$http.post('/api/scrawl/save',{
"aid":"1",
"listname":that.list,
"img":base64Data
}
).then(function(res){
console.log(res)
}).catch(function(error){
console.log(error)
})
代理跨域 打开config 中index.js 找到proxyTable {}修改如下
proxyTable: {
'/api':{
target:'http://www.xxx.com',
changeOrigin:true,
pathRewrite:{
'^/api':'/'
}
}
},
即可