问题背景描述
使用的阿里的OSS上传文件,本地自测通过,但是和前端联调的时候,出现了两个尴尬的问题:
1)一个是跨域问题,413报错
2)另一个是上传1M以内的文件完美上传,超过1M再次报错跨域问题
报错信息:
Access to XMLHttpRequest at 'http://192.168.21.100:9997/wahah/teacher/uploadPicture' from origin 'http://localhost:9000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
跨域解决措施
因为我使用的是Springboot框架,针对于跨域,Boot是有完整解决方案的:
让启动类继承WebMvcConfigurerAdapter类,并且重写addCorsMappings方法:
@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
@Slf4j
public class NewOfficialWebsiteApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(NewOfficialWebsiteApplication.class, args);
}
@Overrid