问题描述:
has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
这个问题通过字面意思,可以看出来是跨域问题,这个是去检查前后端代码,是不是跨域没有设置好, 比如我这次后端的没有弄好,所以就报这种错误。
前端设置
如果是vue的话,就可以如下方式
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api':{
target:'http://localhost:8443', //后期可以改
changeOrigin:true,
pathRewrite:{
'^/api': ''
}
}
}, //配置跨域支持
后端设置
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
}