码迷,mamicode.com
首页 > 其他好文 > 详细

vue跨域配制

时间:2019-10-28 21:19:49      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:operator   rewrite   log   use   中间   int   文件   err   path   

(第一种方法)

在config文件夹下的index.js中配制

proxyTable: { ‘/api‘: { //使用"/api"来代替"http://f.apiplus.c" target: ‘http://127.0.0.1:8000/‘, //源地址 changeOrigin: true, //改变源 pathRewrite: { ‘^/api‘: ‘‘ //路径重写 } } }

(第二种方法)

用Django的第三方包 django-cors-headers 来解决跨域问题

  • 操作步骤:

  1. pip install django-cors-headers

  2. 在settings.py中添加corsheaders.middleware.CorsMiddleware,在SessionMiddlewareCommonMiddleware的中间

3.在settings.py中添加CORS_ORIGIN_ALLOW_ALL = True

axios

  • 安装 axios

cnpm install --save axios

  • 配制axios

在src文件下的mian.js中配制

import axios from ‘axios‘
Vue.prototype.axios = axios
axios使用
?
axios完整写法:
?
this.axios({
 method: ‘post‘,
 url: ‘/user/12345‘,
 data: {
   firstName: ‘Fred‘,
   lastName: ‘Flintstone‘
}
}).then((res)=>{
     console.log(res)
}).catch((error)=>{
     console.log(error)
});

post请求

this.axios.post(‘‘,{}).then((res)=>{}).catch((error)=>{})

get请求

axios.get(‘/user?ID=12345‘)
.then((response)=> {
console.log(response);
})
.catch((error)=> {
console.log(error);
});

vue跨域配制

标签:operator   rewrite   log   use   中间   int   文件   err   path   

原文地址:https://www.cnblogs.com/huanghaobing/p/11755163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!