vue cli3.0 解决跨域问题和axios等问题,配合Django

1 跨域问题

Django其实不用改,该前端就行了。

1、在项目的根目录建vue.config.js文件,书写如下:

module.exports = {
    devServer: {
        open: true, // 是否自动弹出浏览器页面
        host: "localhost",
        port: '8080',
        proxy: {
            '/api': {
                target: 'http://127.0.0.1:9000', // 请求服务器根路径
                changeOrigin: true, // 是否跨域
                ws: true, // websocket
                pathRewrite: { // 重写路径: 当检测到请求地址里包含 /v1 时,将此路径进行跨域代理操作
                    '^/api': '/api'
                }
            }
        }
    }
}

2、还没完!!!!!!然后注意,要修改请求:

 axios.get('/api/test/').then(res=>{
      console.log(res)
    })

就是将

 axios.get('http://localhost:9000/api/test/').then(res=>{
      console.log(res)
    })

改成上面那样!!!!!!!不改还是会报错的!!!

2 axios

npm add axios

直接在vue文件中用,不要去搞成全局的(修改main),很丑而且很傻逼!


<script>
import axios from 'axios';

export default {
  name: "User",
  data() {
    return {
      page_info: 'This is User Info',
    }
  },
  created() {
    axios.get('/api/test/').then(res=>{
      console.log(res)
    })
  }
}
</script>

你可能感兴趣的:(Django,Vue,vue,js,proxy,python,跨越问题)