转载自:https://segmentfault.com/a/1190000014702852
https://segmentfault.com/a/1190000008629361
https://www.cnblogs.com/snowhite/p/7872503.html
vue跨域解决办法:如果只是在开发环境跨域,那只需前端配置config.js文件下的proxy代理,或者说后台配置都i可以。而真正的生产环境还是需要后端配合,需要通过在服务端设置header('Access-Control-Allow-Origin:*');//允许所有来源访问,header('Access-Control-Allow-Method:POST,GET');//允许访问的方式
我试过的一种方式:https://segmentfault.com/q/1010000012242075 后台设置header https://www.cnblogs.com/wancheng7/p/8987694.html
我试过的方式二:
在config/index.js文件配置代理
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: { //定义开发服务器的代理规则
'/extract': { //将www.exaple.com印射为/apis
target: 'http://192.168.10.27:8380/extract', // 接口的一部分 完整的接口:http://192.168.10.27:8380/extract/userInfo/getSearchInfo.htm
changeOrigin: true, //是否跨域
pathRewrite: {
'^/extract': '' ///extract代表的是http://192.168.10.27:8380/extract
}
}
},
调用接口:
this.$http.post("/extract/userInfo/getSearchInfo.htm",JSON.stringify(searchInfo)).then(function(response){
console.log(response);
})
// Various Dev Server settings
// host: 'localhost', // can be overwritten by process.env.HOST
host: '本机Ip', // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined 开发服务器监听的特定端口
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map',
// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,
cssSourceMap: true
},
网友评论