vue cross-domain and cross-domain django

1, the first cross-domain vue I know two, one in the config file, index.js inside cross-domain configuration:

= {module.exports 
dev: {

// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/ API': {// Use "/ api" instead of "http: //f.apiplus. C "
target: 'HTTP: //127.0.0.1: 8000 /', // source address
changeOrigin: true, // address change
pathRewrite: {
'^ / API': '' // routing rewritable
}
}
},
VUE Code:
mounted() {
this.axios({
url:'/api/sadmin/show/', #后端接口
data:{},
method:'get'
}).then((res)=>{
console.log(res)
if (res.data.code==200){
this.alist = res.data.message
},

2, the other is written in the code vue time: Direct designated routes:
methods: {
login(){
let formdata = new FormData();
formdata.append('accout',this.accout);
formdata.append('password',this.password);
this.axios({
url:'http://127.0.0.1:8000/api/doctorLogin/', #指定后端端口号
method:'post',
data:formdata

}).then((res)=>{
        the console.log (RES) 
}
If the second option will then be disposed in the cross-domain django inside;
= The INSTALLED_APPS [ 
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles' ,
'rest_framework',
'corsheaders', this line across domains registered when #
'weblist'
]

as well as middleware
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware', #配置这一行
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
And do not forget to add api at a primary route
= the urlpatterns [ 
path ( 'ADMIN /', admin.site.urls),
path ( 'API /', the include ( 'weblist.urls')), cross-domain interfaces API #
]

so can! !
 

Guess you like

Origin www.cnblogs.com/pp8080/p/11931473.html