vue跨域問題解決(使用webpack打包的)
-
配置代理:(config下index.js文件)
module.exports = { dev: { env: require('./dev.env'), autoOpenBrowser: true, assetsSubDirectory: 'static', assetsPublicPath: '/', //在dev中添加下面的塊 proxyTable: { '/api': { // target: 'http://47.100.34.99', //目標接口域名 target: 'http://10.230.152.141:8080/', changeOrigin: true, //是否跨域 secure: false, pathRewrite: { '^/api': '' //重寫接口 } }, cssSourceMap: false }, ... }
-
配置main.js
import axios from 'axios' // 設置axios請求的token axios.defaults.headers.common['token'] = 'f4c902c9ae5a2a9d8f84868ad064e706' axios.defaults.headers.post['Content-type'] = 'application/json;charset=UTF-8'
-
使用
this.$axios.post("/api/greeting", { username: self.name, password: self.password, email: self.email, sex: self.select }) .then(function(res) { alert(res.data+",,,,success") }) .catch(function(error) { }
注:目前只在生產環境測試了,可以跨域
參考博文:https://blog.csdn.net/u012369271/article/details/72848102