角度 - 如何保存从开机自检返回的字符串



我正在尝试保存从HTTP POST返回的字符串值,但它一直未定义。

我的服务:

getString(id: string): Observable<any>{
    const params = {
        Id: id
    }
    return this.http.post(url , null, {params:params});
}

POST 将返回一些随机字符串

dsfsdfsdfsfsadfsdfsdafwrewrtwerctwertwerctwertwetrxwet

在我的组件中:

string1: any;
saveString() {
this.transactionService.getString(this.id).subscribe(data => 
{
        this.string1= data;
    })
}

但是我的string1data返回未定义,HTTP服务成功返回字符串值。如何保存值?

尝试 这设置数据 在订阅任何

string1: any;
saveString() {
    this.transactionService.getString(this.id).subscribe((data:any) => 
    {
        this.string1= data;
    })
}

我认为你的代码是错误的。POST 需要在第二个参数中传递数据,请参阅 https://angular.io/guide/http 中的文档,

/** POST: add a new hero to the database */
addHero (hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
    .pipe(
      catchError(this.handleError('addHero', hero))
    );
}

最新更新