wujie icon indicating copy to clipboard operation
wujie copied to clipboard

主应用内子应用请求接口 axios response 不返回带有token的headers信息,子应用单独启动可以返回完整的,请问我该怎么配置?

Open coder-orange opened this issue 3 years ago • 3 comments

描述bug 主应用内子应用请求接口,axios返回的headers中缺少token信息,导致其他接口报错

主子应用设置 devserver.proxy中的Access-Control-Allow-Headers = ‘*’ 也无效 子应用单独启动可以返回完整headers信息,应该不是后台的原因 请问我该怎么配置?

如何复现

主应用 vue3 + vite 子应用 vue2 + webpack5

1、 主应用内vue.config.js配置

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';

export default defineConfig({
	server: {
		host: 'localhost',
		port: 3000,
		headers: {
			'Access-Control-Allow-Origin': '*',
			'Access-Control-Allow-Headers': '*',
		},
		proxy: {
			'/_nxman': {
				target: 'https://xxxxxx.com', // dev环境
				changeOrigin: true,
				pathRewrite: {
					'/_nxman': '/',
				},
			},
		},
	},
	resolve: {
		alias: {
			'@': path.resolve(__dirname, './src'),
		},
	},
	plugins: [vue()],
})

2、 第二步 子应用内vue.config.js配置

devServer: {
		open: true, 
		host: 'localhost',
		port: '3001',
		https: false,
		hot: true,
		compress: true,
		headers: {
			'Access-Control-Allow-Origin': '*',
			'Access-Control-Allow-Headers': '*',
			'Access-Control-Allow-Methods': '*',
		},
		proxy: {
			'/_nxman': {
				target: 'https://xxxxx.com', // dev环境
				changeOrigin: true,
				pathRewrite: {
					'/_nxman': '/',
				},
				onProxyRes: function (proxyRes, req, res) {
					proxyRes['Access-Control-Allow-Credentials'] = true;
					proxyRes['Access-Control-Allow-Origin'] = req.headers.origin || '*';
					proxyRes['Access-Control-Allow-Headers'] = '*';
					proxyRes['Access-Control-Allow-Methods'] = 'PUT,POST,GET,DELETE,OPTIONS';
					proxyRes['Content-Type'] = 'application/json; charset=utf-8';
				},
			},
		},
	}

错误截图

主应用错误截图 main-error

子应用单独启动截图 subapp-error 最小复现仓库或者地址 重要!!!,请尽量给出复现仓库,这样能极大加快bug解决速度

coder-orange avatar Jan 10 '23 09:01 coder-orange

image 配置跟你一致,我这主应用访问子应用接口还存在跨域,我该如何改

yszhangjl avatar Feb 08 '23 09:02 yszhangjl

同样遇到了,求解。

imjinbo avatar Aug 02 '23 02:08 imjinbo

同样遇到了,求解。

已破案:https://www.cnblogs.com/testlearn/p/17311664.html

imjinbo avatar Aug 02 '23 06:08 imjinbo