how to set headers to application/json in angular2

前端 未结 5 1156
难免孤独
难免孤独 2021-01-17 03:00

I am trying to send HTTP post request in Angular2 but not able to set headers to content type application JSON.

My code is:

login(url,postdata)
{
            


        
5条回答
  •  悲&欢浪女
    2021-01-17 03:26

    Angular 9 version for 2020

    export class ApiService {
    
      private headers = new HttpHeaders({
        'Content-Type': 'application/json',
      });
    
      constructor(
        private http: HttpClient) {
      }
    
      get(path: string, params: HttpParams = new HttpParams()): Observable {
        return this.http.get(`${environment.apiUrl}${path}`, {params, headers: this.headers});
      }
    }
    

提交回复
热议问题