“Access-Control-Allow-Credentials”问题
Access-Control-Allow-Credentials跨域问题在前端向后端请求接口数据时在控制台报出错误:解决方法:前端:在config文件下的index.js中`module.exports = {dev: {// PathsassetsSubDirectory: 'static',assetsPublicPath: '/',proxyTable: {'/api': {target:
·
Access-Control-Allow-Credentials跨域问题
- 在前端向后端请求接口数据时在控制台报出错误:
- 解决方法:前端:
在config文件下的index.js中`
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://localhost:后端设置的端口号',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
在后端也要配置跨域请求:
@Override
public void addCorsMappings(CorsRegistry registry) {
//所有请求都允许跨域
registry.addMapping("/**")
.allowCredentials(true)
//添加起源
.allowedOrigins("http://localhost:前端端口号")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowedHeaders("*")
.maxAge(3600);
}
注意的是不要忘记添加前端起源
3. 配置完之后问题就得到了解决
更多推荐
已为社区贡献1条内容
所有评论(0)