【问题标题】:angular 4 post request not working, but postman is able to send post request to spring boot server?angular 4 post 请求不起作用,但邮递员能够将 post 请求发送到 spring boot 服务器?
【发布时间】:2018-11-23 18:57:34
【问题描述】:

我能够使用邮递员在 Spring Boot 中成功地向我的 restcontroller 发送一个发布请求。

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

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

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

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

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

谢谢。

【问题讨论】:

  • 从 Angular 发送请求时会发生什么?它显示了什么错误?
  • 如果任何答案有帮助,请尝试进行投票,如果答案正确,则标记为已接受,因为其他开发人员可以先看到该答案。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});
    

    应该是-

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2019-12-30
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      相关资源
      最近更新 更多