vue中axios跨域请求解决
一,首先在文件中下载axios
cnpm install axios --save-dev
二、在main.js中引入及获取对象
import Vue from 'vue'
import App from './App'
import router from './router'
import Axios from 'axios'
Vue.prototype.$axios = Axios
三、在config里面的index.js文件里设置proxyTable
const path = require('path')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://api.douban.com/v2',//此处可以换成自己需要的地址
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
},
四、main.js里添加host
五、数据请求
that.axios({
method:'post',
url:that.Host+'/sendSms',
// url:api.sendSms,
data: {mobile:mobile2},
headers:headers(),
cache:false
}).then(function(res){
console.log(res.data);
Indicator.close();
if(res.errcode == '1002'){
that.$toast(res.errmsg);
that.timingCode(); //倒计时
}else{
that.$toast(res.errmsg);
that.codeState=true;
that.codeState2=false;
}
}).catch(error => {
});
注意:配置文件后,要重新启动哦!!!