java - 发布约会/预订 - CORS 政策问题
问题描述
我尝试发布带有预订数据的字典。但是chrome记录了这个错误:Access to XMLHttpRequest at 'http://localhost:8080/reservations' 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.
这很奇怪,因为我在@CrossOrigin
控制器上方添加了注释,所以我可以发布图像、视频、html 内容。但是对于这个特定的帖子请求,它似乎不起作用。
休息控制器:
@CrossOrigin(origins="http://localhost:4200",
maxAge=2000,allowedHeaders="header1,header2",
exposedHeaders="header1",allowCredentials= "false")
@RestController
public class ReservationsController {
private ReservationDao dao;
@Autowired
public ReservationsController(ReservationDao dao) {
this.dao = dao;
}
@PostMapping("/reservations")
public Map<String, String> bookReservation(@RequestBody Map<String, String> reservation) {
System.out.println(reservation);
return null;
}
}
angular api bookReservation方法:
bookReservation(data) {
console.log(data);
const result = this.http.post(this.apiUrl + 'reservations', data).subscribe(
(val) => {
console.log('POST call succesful value returned in body',
val);
},
response => {
console.log('POST call in error', response);
},
() => {
console.log('The POST observable is now completed');
});
console.log(result);
}
解决方案
如果您仅设置 allowedHeaders ,您将允许此参数,如果它接收其他参数,则它永远不会发送交叉源头,并且 chrome 将抛出错误。
如果不需要,您应该删除 allowedHeaders、exposedHeaders 和 allowCredentials。
推荐阅读
- java - 如何将我的枚举代码转换为开关
- python - 使用 Mysys2 时为 GTK3+/PyGObject 使用哪个 Python 解释器
- python - 在 docker 镜像中安装 pyodbc
- ios - react-native xcode 10.1“config.h”文件未找到
- mysql - WordPress docker 容器不以 mysql:latest image 开头
- c# - 如何使用 MongoDB (ASP.NET Core 2.2) 访问深度嵌套的数组
- machine-learning - KNN算法在训练阶段做了什么?
- python - 为什么我的 ModelChoiceField 显示三个表单而不是两个表单,为什么它不读取我的选择?
- python-3.x - 选择导入文本文件的特定列
- python - discord.py 缺少类别 api 和枚举 def