Angular请求http方法Get



我有一个问题,通过post方法的http请求。然后发送到具有映射为

的端点的后端我CarController

@RequestMapping(value = "/car")
@AllArgsConstructor
@CrossOrigin
public class CarController {
@PostMapping(value = "/test/{code}")
public ResponseEntity<String> getCars(@PathVariable(required = true) String code) throws URISyntaxException {
return ResponseEntity.ok("Response OK");
}
}

我的环境
export const environment = {
production: false,
apiEndpoint: "http://localhost:8080/my-app/"
};

我app.service.component.ts

httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
};
getCars() {
const url = `${environment.apiEndpoint}`+ "/car/teste/?" + `${code}`;
this.http.get<any>(url).subscribe(resp => {
console.log(resp)
});
}

错误日志

请求有404错误,并带有以下消息

错误{身体:{…},url: http://localhost: 8080/车/测试/',标题:HttpHeaders, status: 404, statutext: 'Not Found'}正文:{错误:收集'car'未找到"}headers: HttpHeaders {normalizedNames:Map(0), lazyUpdate: null, lazyInit: {}status: 404statusText: "NotFound" url:"http://localhost: 8080/app/汽车/test"[[原型]]:对象

不确定您是否指的是get请求,因为上面的方法是get请求。

无论如何,仔细检查你的URL拼写,在API端点,URL/test/{code},然而在客户端,URL".../teste/?"...

teste中的e可能导致404

您将代码参数作为Query参数传递,而不是像路由参数那样传递。这就是为什么它找不到车。

最新更新