如何使用 HttpRepsonse 从 post 方法获取 return

How get a return from post method with HttpRepsonse

我无法从 return 消息中获取 headers.get(状态)的结果。

我发现可能是因为我的http.post没有Httpresponse所以angular无法获取headers信息。

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,我想用它来做不同的通知。

请先使用 HttpClient 而不是 http,因为 http 已被弃用。

从“@angular/common/http”导入 {HttpClient、HttpRequest};

示例: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;
      }));