angular 4 post请求不起作用,但邮递员能够发送邮件请求到spring boot服务器吗?

时间:2018-06-14 05:13:21

标签: angular spring-mvc spring-boot cross-domain postman

我可以使用postman在spring boot中成功发送一个post请求到我的restcontroller。 enter image description here

restvtroller如下:

@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("*");
            }
        };
    }

但是我无法从我的角度发送此帖子请求,请求如下:

const headers = new HttpHeaders().set("Content-Type", "application/json");

    return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});

注意:我已经从here添加了WebMvcConfigurer!

感谢。

2 个答案:

答案 0 :(得分:2)

不使用subscribe方法,您的api永远不会被调用。只需在组件中使用订阅方法。

答案 1 :(得分:0)

return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});

应该是 -

return this.http.post(this.performactionUrl, JSON.stringify({"bladeId" : 2,"actionName" : "reboot"}),headers);
相关问题