【问题标题】:Post Request returns an error observable in Angular despite having been successfully executed at the back-end yet postman returns the correct response尽管已在后端成功执行,但 Post Request 在 Angular 中返回可观察到的错误,但邮递员返回正确的响应
【发布时间】:2019-09-29 11:34:34
【问题描述】:

尽管已在服务器端成功执行,但我的所有 Angular 发布请求都返回可观察到的错误。我不知道我做错了什么。

组件:

 this.postInit.postInitialSetup(this.userForm).subscribe(

    (data: string) => {

      if (data === 'Posted Successfully') {
        this.userForm.reset();
        this.router.navigate([this.path]);
      }
    },

    (error: any) => {
      this.errored = true;
      this.serviceErrors = error;

    }

  );

发出 HTTP 请求的服务:

import { Injectable } from '@angular/core';
 import { HttpErrorResponse, HttpClient, HttpHeaders } from 
 '@angular/common/http';
import { map, catchError } from 'rxjs/operators';
 import { FormGroup } from '@angular/forms';
  import { throwError } from 'rxjs';

 @Injectable({
providedIn: 'root'
})
 export class SetupService {

initialSetUpUrl = 'api/initialSetup';
httpOptions = {
headers: new HttpHeaders({
  'Content-Type': 'application/json'
})
 };
 constructor(private http: HttpClient) { }



postInitialSetup(postData: FormGroup) {

return this.http.post(this.initialSetUpUrl, postData.value, 
this.httpOptions).pipe(

  map((data: string) => {
    return data;
  }),


  catchError(this.handleError)

);

  }



 private handleError(errorResponse: HttpErrorResponse) {

if (errorResponse.error instanceof ErrorEvent) {
  // A client-side or network error occurred. Handle it accordingly.
  console.error('An error occurred:', errorResponse.error.message);
} else {
  // The backend returned an unsuccessful response code.
  // The response body may contain clues as to what went wrong,
  console.error(
    `Backend returned code ${errorResponse.status}, ` +
    `body was: ${errorResponse.error}`);
  }
  // return an observable with a user-facing error message
  return throwError('Something bad happened; please try again later.');
}

}

这是发送到服务器的数据

这是发帖后的回复:

考虑到对于 angular 和整个 observables 来说相对较新,有人可以帮我确定我在服务代码或组件代码中可能做错了什么。

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    我猜你的问题是你希望.subscribe() 中的数据是一个字符串:

     (data: string) => {
        ...
     },
    

    你应该有一个HttpResponse 对象。

    然后你可以检查:

     (data: HttpResponse) => {
        if (data.status === 200) { // do something }
     }
    

    【讨论】:

    • 有这个通用类型 'HttpResponse' 需要 1 个类型的参数。ts(2314) 我不知道如何处理它
    • @Googo 您是否尝试过提供类型参数,如the docs 所示?
    • this.postInit.postInitialSetup(this.userForm).subscribe( (data: HttpResponse) => { if (data.status === 200) { this.userForm.reset() ; this.router.navigate([this.path]); } }, (error: HttpErrorResponse) => { this.errored = true; this.serviceErrors = error; } );这就是我实现它的方式,但它的行为仍然不正确
    • @Googo 当您不知道必须使用哪种参数作为参数时,您也可以使用“HttpBaseResponse”。
    • this.postInit.postInitialSetup(this.userForm).subscribe( (data: HttpResponseBase) => { if (data.status === 200) { this.userForm.reset(); this. router.navigate([this.path]); } }, (error: HttpErrorResponse) => { this.errored = true; this.serviceErrors = error; } );我仍然遇到同样的错误
    猜你喜欢
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2011-02-24
    • 2019-09-25
    • 2018-02-21
    相关资源
    最近更新 更多