问题与响应(POST 应用程序/json).400 错误请求



我有代码:

1) 阿贾克斯

addFarmByPost = (e) => {
        e.preventDefault();
        alert(JSON.stringify(this.state));
        fetch("http://localhost:8080/farms", {
            method: "POST",
            headers: {
                'Content-Type': 'application/json'
            },
            data: JSON.stringify(this.state)
        })
    }

2) 休息控制器

 @PostMapping("/farms")
    public ResponseEntity<Object> addFarm(@RequestBody String farmParam) {...//do }

3) 弹簧启动应用程序中的Clobal cors配置:

**@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter(){
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedMethods("POST", "GET", "PUT", "DELETE", "OPTIONS")
                        .allowedOrigins("*")
                        .allowedHeaders("*")
                        .allowCredentials(false)
                        .maxAge(6000);
            }
        };
    }**

但是当我发出ajax POST请求(应用程序/json)时,我有exeption 400错误请求。异常:处理程序执行:org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文。

获取 CORS 方法效果很好。我如何解决它并了解错误的原因。泰。

例外在这里data: JSON.stringify(this.state)必须body: JSON.stringify(this.state)因为我使用 reactJS,所以我必须编写body而不是在 ajax 请求中data

最新更新