VUE 前后端分离两个问题

2017-12-26 22:17:10 +08:00
 SlipStupig

我现在项目,前端使用 vue 做页面,后端使用 python

  1. npm run build 后无法显示背景图片(背景图片使用 CSS 写在模板的style标签里面的)

2.使用 axio 请求后端无法将 cookie 设置到浏览器,网上看了一圈方法都不行,withCredentials=true也设置了,但是提示我错误

Failed to load http://localhost:8088/g: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8088' that is not equal to the supplied origin. Origin 'http://localhost:8080' is therefore not allowed access.

后端设置:

        self.set_header('Access-Control-Allow-Credentials', True)
        self.set_header("Access-Control-Allow-Origin", "http://localhost:8088")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')

怎么才能解决呢?

12547 次点击
所在节点    Node.js
32 条回复
yanqing07
2017-12-27 09:59:48 +08:00
你收不到 cookie。也可能是 vue 的 ajax 发送时,没带 cookie。不一定是 py 的锅
SlipStupig
2017-12-27 11:04:16 +08:00
@changkong 改了后给个个 warning

Tip: built files are meant to be served over an HTTP server.
Opening index.html over file:// won't work.
vivinBear
2017-12-27 11:11:40 +08:00
main.js
Axios.defaults.withCredentials = true

build/utils.js

if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
publicPath: '../../' /// 加上这一行
})
} else {
return ['vue-style-loader'].concat(loaders)
}

config/index.js
把 build 的 assetsPublicPath 的值改成""
就可以了
vivinBear
2017-12-27 11:12:07 +08:00
@SlipStupig 看我上面的,你的这些问题我都遇到过,并且都解决了
vivinBear
2017-12-27 11:12:16 +08:00
亲测可行
moxiaonai
2017-12-27 11:29:25 +08:00
楼上正解。样式问题是编译后的路径不对

跨域问题,服务端设置 cors。'Access-Control-Allow-Origin'必须为具体域名,不能为*。
前端 Axios.defaults.withCredentials = true,也必须允许接收跨域的 cookie

SlipStupig
2017-12-27 11:33:12 +08:00
@vivinBear 能留个联系方式不,我还是没解决
SlipStupig
2017-12-27 11:37:22 +08:00
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pkb2N1bWVudElEPSJ4bXAuZGlkOkY1QTRERkEwNkZGMTExRTFBQjBCODVFNEE2MzdFRDhGIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+5Xmx/AAAAF5JREFUeNrsztsJgFAMwNDWFyIiCOL+m1aXEOM1gfyfjIgzwOX9QQfudOBGB6504EIHznTgRAeOdOAgsHVgL1Dgy8BOoECBAp8FpkCBAj8MtObLqhIoUKDAPwMvAQYAhqcYrJBlYv8AAAAASUVORK5CYII=),跟我设置的完全不一样。我的配置

build/utils.js:
// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
publicPath: '../../' /// 加上这一行
})
} else {
return ['vue-style-loader'].concat(loaders)
}

config/index.js:
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: "",

/**
* Source Maps
*/

productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',

// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],

// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}

CSS:
body {
height: 100%;
width: 100%;
position: fixed;
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
background: url('../assets/login_bg.jpg') no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
vivinBear
2017-12-27 13:41:11 +08:00
@SlipStupig 那这个就和前端没什么关系了吧,你现在还有什么问题,把错误贴出来
SlipStupig
2017-12-27 14:52:38 +08:00
@vivinBear 背景图片无法显示啊
vivinBear
2017-12-27 21:27:38 +08:00
@SlipStupig 你给我个联系方式,然后我试着帮你看看
v2gg
2017-12-28 08:38:40 +08:00
@SlipStupig build 之后显示不出来可以 F12 看看是什么原因,可能是相对路径的锅

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/417855

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX