本文介绍了带有JSON参数的Typescript Rest API POST调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到一个代码示例,以JSON作为参数将POST请求发送到REST Api,并获取响应以及Typescript中的任何异常/错误代码.
I'm trying to find a code example to send a POST request to a REST Api with a JSON as parameter and get the response and any exceptions / error codes in Typescript.
推荐答案
您可以从类似的内容开始
You could start with something like that
服务
export class MyService{
constructor(
private httpClient: HttpClient) {
}
getSomethingFromServer():Observable<any>{
return this.httpClient.get('you_request_url');
}
}
组件
constructor(
private myService: MyService) {
}
this.myService.getSomethingFromServer().subscribe(response => {
// do whatever you want with the response
}, error => {
// handle error here
// error.status to get the error code
});
这篇关于带有JSON参数的Typescript Rest API POST调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!