[英]Angular Spring Boot: Redirection error: Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response
I'm using Angular 6 in the frontend and Java Spring Boot in the backend. 我在前端使用Angular 6,在后端使用Java Spring Boot。
I want my login page to make request to server. 我希望我的登录页面向服务器发出请求。 Server redirects to dashboard page. 服务器重定向到仪表板页面。
I have http://localhost:4200/login angular page which makes a POST request to server http://localhost:8080/login 我有http:// localhost:4200 / login角度页面,该页面向服务器http:// localhost:8080 / login发出POST请求
The .ts file contains: .ts文件包含:
onLoginClickPost() {
console.log('redirect request clicked');
this.data.getUserInfoPost().subscribe(
data => {
console.log(data);
this.userInfo = data;
}
);
}
getUserInfoPost() {
return this.http.post('http://localhost:8080/login', {nextUrl: 'http://localhost:4200/dashboard'});
}
The server has @RestController with a method mapping to incoming POST request, which should redirect to http://localhost:4200/dashboard : 服务器具有@RestController,其方法映射到传入的POST请求,该方法应重定向到http:// localhost:4200 / dashboard :
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<?> login_post(@RequestBody LoginReqPostParam payload) {
HttpHeaders headers = new HttpHeaders();
try {
headers.setLocation(new URI(payload.getNextUrl()));
headers.add("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
} catch (URISyntaxException e) {
logger.error("URI Exception: ", e);
e.printStackTrace();
}
return new ResponseEntity<>(headers, HttpStatus.FOUND);
}
LoginReqPostParam is a simple class to have incoming JSON payload as object. LoginReqPostParam是一个简单的类,将传入的JSON有效负载作为对象。
The login angular page is able to make POST request to the server. 登录角度页面能够向服务器发出POST请求。 Now at the server, after returning the ResponseEntity from server, I'm getting following error in browser console: 现在在服务器上,从服务器返回ResponseEntity之后,在浏览器控制台中出现以下错误:
Failed to load http://localhost:4200/dashboard : Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response. 无法加载http:// localhost:4200 / dashboard : 预检响应中Access-Control-Allow-Headers不允许请求标头字段Content-Type。
How do I redirect to http://localhost:4200/dashboard page? 如何重定向到http:// localhost:4200 / dashboard页面? Do I need to add something to angular dashboard component files? 我是否需要向角度仪表板组件文件中添加一些内容?
You are using @CrossOrigin
for POST request but Angular (in development) is sending a Preflight request using OPTIONS. 您正在使用@CrossOrigin
进行POST请求,但是Angular(正在开发中)正在使用OPTIONS发送预检请求 。
I recommend you to use a development profile that permit all origins (you must create a custom filter in a Bean for these profile) 我建议您使用允许所有来源的开发配置文件(必须在Bean中为这些配置文件创建自定义过滤器)
Use Allow-Control-Allow-Origin: * extension for google chrome. 使用Allow-Control-Allow-Origin:* Google chrome扩展名。 And enable cross origin resource sharing https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en 并启用跨源资源共享https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=zh-CN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.