代码之家  ›  专栏  ›  技术社区  ›  W Kenny

如何使用httprepson从post方法获取返回

  •  -2
  • W Kenny  · 技术社区  · 7 年前

    我无法获取邮件头的结果。请从返回邮件中获取(状态)。

    我发现这可能是因为我的http.post没有httpresponse,所以angular无法获取标题信息。

    服务:

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

    }

    组件:

    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 回复  |  直到 7 年前
        1
  •  0
  •   Vishal Sharma    7 年前

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

    导入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;
          }));
    
    推荐文章