angular 4 post 请求不工作,但是 post 人能够发送 post 请求到 spring 引导服务器?
angular 4 post request not working, but postman is able to send post request to spring boot server?
我能够使用 postman 在 spring 引导中成功向我的 restcontroller 发送 post 请求。
restcontroller如下:
@RequestMapping(value="/performaction",method=RequestMethod.POST,consumes = "application/json", produces = "application/json")
public void performReboot(@RequestBody PerformAction performAction) {
System.out.println("......rebooting blade to performRebootservice...........for blade id :: : ::"+performAction.getBladeId() +performAction.getActionName());
..........
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
但是我无法从我的 angular 发送这个 post 请求,请求如下:
const headers = new HttpHeaders().set("Content-Type", "application/json");
return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});
注意:我添加了来自 !
的 WebMvcConfigurer
谢谢。
如果不使用 subscribe
方法,您的 api 永远不会被调用。只需在您的组件中使用订阅方法。
return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});
应该是-
return this.http.post(this.performactionUrl, JSON.stringify({"bladeId" : 2,"actionName" : "reboot"}),headers);
我能够使用 postman 在 spring 引导中成功向我的 restcontroller 发送 post 请求。
restcontroller如下:
@RequestMapping(value="/performaction",method=RequestMethod.POST,consumes = "application/json", produces = "application/json")
public void performReboot(@RequestBody PerformAction performAction) {
System.out.println("......rebooting blade to performRebootservice...........for blade id :: : ::"+performAction.getBladeId() +performAction.getActionName());
..........
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
但是我无法从我的 angular 发送这个 post 请求,请求如下:
const headers = new HttpHeaders().set("Content-Type", "application/json");
return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});
注意:我添加了来自
谢谢。
如果不使用 subscribe
方法,您的 api 永远不会被调用。只需在您的组件中使用订阅方法。
return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});
应该是-
return this.http.post(this.performactionUrl, JSON.stringify({"bladeId" : 2,"actionName" : "reboot"}),headers);