根目录config文件夹:
proxyTable 是 webpack-dev-server 的功能,应该把相关配置放到 Webpack 配置里。
proxyTable: {
'/api': {
target: 'http://127.0.0.1:8011/', //这里后台的地址模拟的;应该填写你们真实的后台接口
changOrigin: true, //允许跨域
pathRewrite: {
/* 重写路径,当我们在浏览器中看到请求的地址为:http://localhost:8080/api/core/getData/userInfo 时
实际上访问的地址是:http://121.121.67.254:8185/core/getData/userInfo,因为重写了 /api
*/
'^/api': ''
}
},
},
dev里面:
proxyTable只用写api那条就行了
访问地址:api/accounts/login/
POST:
this.$http.post("api/accounts/login/", {name: this.form.name, password: this.form.password }, {emulateJSON: true}).then((response) => {
console.log(response.body);
this.user = response.body.user;
for (var i = 0; i < this.user.length; i++) {
if (this.user[i].name === this.form.name && this.user[i].password === this.form.password) {
this.form.type = this.user[i].type;
this.setUserInfo(this.form);
this.$router.push({path: 'home'});
return;
}
}
this.tips = '密码不正确!';
this.$message({
message: this.tips,
type: 'warning'
});
}, response => {
this.$message({
message: '数据请求失败',
type: 'error'
});
});
修改完后要重启 npm run server 才能生效
可以看到/app被正则替换为空字符,实际访问的接口是http://127.0.0.1:8000/accounts/login/
Django跨域设置:
注释掉setting.py的MIDDLEWARE的’django.middleware.csrf.CsrfViewMiddleware’