【发布时间】:2018-07-11 12:29:47
【问题描述】:
我有一个弹簧靴admin server 和一个angular-client 前部。我正在尝试使用 HTTPClient 将一些数据从我的前端发送到我的服务器,但不知何故,我在请求期间收到以下错误,但首先是我的代码:
angular-client 中的 POST 请求:
runBatch(id: number, stateUpd: string): Observable<HttpEvent<{}>> {
const req = new HttpRequest('POST', '/update_state', {id, stateUpd}, {
reportProgress: true,
responseType: 'text'
});
return this.http.request(req);
}
angular-client 中的控制器:
changeBatchState(state: string): void {
if(this.selection.selected.length >= 1){
this.selection.selected.forEach(batchInstance =>{
if(batchInstance.batchState == 'CRASHED' || 'KILLED' || 'SUBMITTED'){
console.log(batchInstance.id + " setting to RUNNABLE...");
this.dataTableService.runBatch(batchInstance.id, state).subscribe(event => {
if(event.type === HttpEventType.UploadProgress) {
console.log('POST /update_state sending...');
}
else if(event instanceof HttpResponse) {
console.log('Request completed !');
}
});
}
else {
console.log(batchInstance.id + ' can not set to RUNNABLE');
}
});
}
}
admin server 中的控制器:
@PostMapping("/update_state")
public ResponseEntity<String> batchChangeState(@RequestParam("id") int id, @RequestParam("stateUpd") String stateUpd) {
try {
log.i("INSIDE CONTROLLER");
log.i("BATCH INSTANCE ID : " + id);
log.i("UPDATE REQUESTED : " + stateUpd);
return ResponseEntity.status(HttpStatus.OK).body("Batch instance: " + id + " updated");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("Fail to update batch instance " + id);
}
}
这里是我在请求期间得到的错误:
ERROR
Object { headers: {…}, status: 400, statusText: "OK", url: "http://localhost:8870/update_state", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:8870/update_state: 400 OK", error: "{\"timestamp\":1517473012190,\"status\":400,\"error\":\"Bad Request\",\"exception\":\"org.springframework.web.bind.MissingServletRequestParameterException\",\"message\":\"Required int parameter 'id' is not present\",\"path\":\"/update_state\"}" }
我不明白它来自哪里,因为我在我的 POST 请求中正确发送了 id,有什么想法吗?
【问题讨论】:
-
您可以在开发者控制台中查看您的发帖请求吗?您可以尝试发送这样的参数: new HttpRequest('POST',
`/update_state?id=${id}&stateUpd=${stateUpd}`,... -
我正在尝试这样的错误:
Error parsing HTTP request header现在正在调试以向您提供请求表.. -
它不会用 ${id} 和 ${stateUpd} 的值替换它们
标签: angular http typescript spring-boot httpclient