gpt4 book ai didi

java - 向服务器发送 POST 请求,服务器的响应为空,Angular - Spring-boot

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:16 24 4
gpt4 key购买 nike

我想用我的 Angular 客户端向我的 spring-boot 服务器发送一个 POST 请求。

服务器成功接收到数据和带有 200 代码的 anwser 以及带有包含“ok”的字符串的正文。

但客户端收到 null 作为 anwser。

Angular 代码:

httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Methods': 'POST, GET'
})
};
this.http.post(this.URL, JSON.stringify(this.alert), this.httpOptions)
.subscribe((result: string) => {
if (result == null || result === 'error') {
//if fail;
} else {
//if success;
}
console.log('result :' + result);
})

Spring 启动代码:

    @CrossOrigin(origins = "http://localhost:9000")
@PostMapping(value = "/url")
public ResponseEntity objDataController(@RequestBody ObjData objData) {
HttpHeaders resHeader = new HttpHeaders();

resHeader.set("origin", "http://localhost:8080");
resHeader.set("Content-Type", "application/json");
resHeader.set("Accept", "application/json");

//some code

return ResponseEntity.ok()
.headers(resHeader)
.body("ok");
}

在 console.log 得到:结果:null

谢谢

最佳答案

有一个类似的问题,它有助于指定响应类型:

const headers = new HttpHeaders({'Content-Type': 'application/json'});
this.httpClient.delete(url, {headers: headers, responseType: 'text'});

关于java - 向服务器发送 POST 请求,服务器的响应为空,Angular - Spring-boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57658288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com