1.

Describe briefly how to create a service with get and post requests.

Answer»

Generate a service first using ionic generate service command. Import HTTPCLIENT from @angular/common/http library. Then add HttpClient to constructor so it can be USED in the service. A typical get and post request can be written as follows : 

get(ENDPOINT: string, params?: any, options?: RequestOptions) {  if (!options) {  options = NEW RequestOptions();  } if (params) {  let p = new URLSearchParams();  for (let K in params) {  p.set(k, params[k]);  } options.search = !options.search && p || options.search;  } return this.http.get(this.url + '/' + endpoint, options);  } post(endpoint: string, body: any, options?: RequestOptions) {  options = new RequestOptions();  options.headers = this.createHeaders();  return this.http.post(this.url + '/' + endpoint, body, options);  }


Discussion

No Comment Found