然后再次在浏览器中测试,发现浏览器提示,当Access-Control-Allow-Credentials设为true的时候,Access-Control-Allow-Origin不能设为星号,既然不让我设为星号,我就改成前端项目的配置
response.setHeader("Access-Control-Allow-Origin", http://127.0.0.1:8010);发现每次ajax请求,还是不同的session,打开chrome的network,发现每次请求的请求头中并没有,和我想象的一样携带cookie信息,也就是下面这个header:
Cookie:JSESSIONID=node015f4w1j2kgjk61i7jyyim8lo3u0.node0;因为我用的axios,所以找到axios的文档链接描述
发现一下内容:
// `timeout` specifies the number of milliseconds before the request times out.
// If the request takes longer than `timeout`, the request will be aborted.
timeout: 1000, // `withCredentials` indicates whether or not cross-site Access-Control requests
// should be made using credentials
withCredentials: false, // default
// `adapter` allows custom handling of requests which makes testing easier.
// Return a promise and supply a valid response (see lib/adapters/README.md).
adapter: function (config) {
/* ... */
},
withCredentials默认是false,意思就是不携带cookie信息,那就让它为true,我是全局性配置的,就是main.js中的这句话:
axios.defaults.withCredentials=true;然后再测试,发现每次ajax请求都是同样的session了(不包含浏览器的options请求)。
3、代理配置
因为不想每个页面里的请求都写http://127.0.0.1:8080,并且我用的是element ui的webpack项目模板,所以就想使用代理(不知道叫这个合适不合适):
devServer: {
host: '127.0.0.1',
port: 8010,
proxy: {
'/api/': {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
pathRewrite:{
'/api':'/xxxxxx'
}
}
}把ajax请求改成下面这个样子:
this.$axios.post('/api/xx/xxx', {}, {
headers: {
"Content-Type": "application/json;charset=utf-8"
}
}).then(function(response) {
// 这里是处理正确的回调 }).catch(function(response) {
// 这里是处理错误的回调
console.log(response)
});
网上说都是配置为proxyTable, 用的是http-proxy-middleware这个插件,我装上插件,改成这个,webpack总是报错,说proxyTable是非法的配置,无法识别。
无奈改成了模板自带的proxy,可以使用,为什么可以用,我还不请求,proxyTabel为什么不能用,也没搞明白。有知道的,可以指点一下。










