【发布时间】:2020-05-06 08:16:12
【问题描述】:
我有一个 SpringBoot 控制器如下
@CrossOrigin(origins="*")
public class FormController {
@PostMapping(path="/basicForm")
public String postResponseController(
@RequestBody FormDomain loginForm) {
System.err.println("basic form method called");
return "file transfer completed successfully";
}
}
我已使用 @CrossOrigin(origins="*") 注释控制器以启用 CORS。
这是我的 FormDomain 类
public class FormDomain {
private MultipartFile fileInput;
public MultipartFile getFileInput() {
return fileInput;
}
public void setFileInput(MultipartFile fileInput) {
this.fileInput = fileInput;
}
}
还有调用控制器方法的角脚本
onClickSubmit(data) {
this.http.post("http://localhost:8080/basicForm", data).subscribe( (ob)=>(console.log(ob)));
}
尽管我已注释控制器以启用 CORS,但我在浏览器控制台中收到以下错误
Access to XMLHttpRequest at 'http://localhost:8080/basicForm' from origin 'http://localhost:4200' 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.
如何解决这个问题?
【问题讨论】:
-
在
FormController中添加@RestController -
在浏览器中使用开发者工具,检查是否发送了标头
标签: angular spring spring-boot