使用@Crossorigin在控制器级别运行Angular的CORS问题
在呼叫到Spring Boot 2.6.4 Web Service
这是Angular 13中的代码:
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT'
});
let options = { headers: headers };
this.http.get<any>("http://localhost:8080/country/available", options).subscribe(data => {
console.log(data);
});
我有@Crossorigin
> controlter 在控制器级别:
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("country")
public final class CountryController {
private final transient CountryService countryService;
@Autowired
public CountryController(final CountryService countryService) {
this.countryService = countryService;
}
@GetMapping("/available")
public List<Country> getAvailableCountries() {
return countryService.getAvailableCountries();
}
}
I'm getting a CORS error when running calls to the Spring Boot 2.6.4 web service
Here is the code in Angular 13:
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT'
});
let options = { headers: headers };
this.http.get<any>("http://localhost:8080/country/available", options).subscribe(data => {
console.log(data);
});
I have the controller annotated with @CrossOrigin
at the controller level:
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("country")
public final class CountryController {
private final transient CountryService countryService;
@Autowired
public CountryController(final CountryService countryService) {
this.countryService = countryService;
}
@GetMapping("/available")
public List<Country> getAvailableCountries() {
return countryService.getAvailableCountries();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下
@crossorigin(origins =“*”,washeheaders =“*”,maxage = 3600)
,或者您可以在安全配置中作为http.cros()
Try this
@CrossOrigin(origins = "*", allowedHeaders = "*" ,maxAge = 3600)
or you can do it in the security configuration as http.cros()