Как получить возврат из почтового метода с HttpRepsonse - PullRequest
0 голосов
/ 23 декабря 2018

Я не могу получить результат для headers.get (status) из ответного сообщения.

Я обнаружил, что, возможно, это потому, что мой http.post не имеет Httpresponse, поэтому angular не может получитьинформация заголовков.

service.ts:

onCreateData(service: LoginFormService) {

const request = JSON.stringify(
  { dataYear: service.form.get('dataYear').value,
    dataMonth: service.form.get('dataMonth').value,
    population: service.form.get('population').value,
    sources: service.form.get('sources').value
  }
);

return this.http.post(this.createDataUrl, request, httpOptions)
  .pipe(
    catchError(this.handleError('onCreateData'))
  );

}

component.ts:

onSubmit() {
this.service.onCreateData(this.service).subscribe(

  (res: Response) => {
    console.log('-----');
    console.log(res.headers.get(status));
    this.service.form.reset();
    this.service.initializeFormGroup();
    this.notificationService.success(':: Submitted successfully');
    this.dialogRef.close();
  }
);

}

Если я могу получить headers.status, я хотел бы использовать его, чтобы сделать другое уведомление.

1 Ответ

0 голосов
/ 23 декабря 2018

Сначала используйте HttpClient вместо http, потому что http устарел.

import {HttpClient, HttpRequest} из '@ angular / common / http';

Пример: 1

 const req = new HttpRequest('POST','YOUR_URL',POST_DATA,{reportProgress: true});
return this.http.request(req);

Пример: 2

this.http.post<Type of response you will get>('YOUR_URL',{
          observe: 'body',
          responseType: 'json'
        })
    }),map(
      ( response)=>{
       ///******************Do some Changes or check in Response************///
        return  response;
      }));

Пример: 3

this.http.post('YOUR_URL',{
          observe: 'body',
          responseType: 'json'
        })
    }),map(
      ( response)=>{
       ///******************Do some Changes or check in Response************///
        return  response;
      }));
...