首页 > 解决方案 > 带有 JSON 参数的 Typescript Rest API POST 调用

问题描述

我正在尝试找到一个代码示例,以使用 as 参数向 Api发送POST请求,并在 Typescript 中获取响应和任何异常/错误代码。RESTJSON

标签: jsonangulartypescript

解决方案


你可以从这样的事情开始

服务

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
  });

推荐阅读