【问题标题】:Angular httpclient.post does not create the right bodyAngular httpclient.post 没有创建正确的正文
【发布时间】:2019-01-10 17:22:08
【问题描述】:

我有一个 Angular 服务,它必须通过 post call 发送一些数据。

我要发的课是:

   export class Informazioni {
   partitaivacf:string;
   idpaese:string;
   }

模板:

          <label for="idpaese">ID</label>
          <input #idpaese type="text" placeholder="ID Paese" [(ngModel)]="informazioni.idpaese">

          <label for="partitaivacf">Partita iva o codice fiscale</label>
          <input #partitaivacf type="text" placeholder="PIVA" [(ngModel)]="informazioni.partitaivacf">

组件:

      export class ImpostazioniFattureComponent implements OnInit {

      informazioni:Informazioni;



      constructor(private informazioniservice:InformazioniService) {
      this.informazioniservice.prendiinformazioni().subscribe(result => {
      if(result != undefined) {
      this.informazioni = result;
      }
      });
      }

      ngOnInit() {}


       salva(){
      this.informazioniservice.salvainformazioni(this.informazioni);
      }

还有服务:

     export class InformazioniService {

     endpoint = 'http://localhost:8080/';

     constructor(private http: HttpClient) { }

     salvainformazioni(info:Informazioni){

     var post = this.http.post(this.endpoint + 'salvainformazioni', info);
     console.log(post);
     post.subscribe( result => {
     });
     }

     prendiinformazioni(): Observable<Informazioni>{
     return this.http.get<Informazioni>(this.endpoint + 'prendiinformazioni');
     }

}

对于其他对象,请求的主体类似于

  ClassName 
  {
   "attr1":"val1",
   ...
   "attrN":"valN"
  }

在这种情况下,如果我在控制台上打印 post 对象,它只是

  {  
   "idpaese":"id",
   "partitaivacf":"piva"
  }

显然,当我打开模板的页面时,会抛出一个错误,即模板无法读取未定义信息对象的属性。可能是与之相关的生成体的问题吗?或者可能是我忽略的另一个问题?

提前致谢

【问题讨论】:

    标签: angular post httpclient


    【解决方案1】:

    看来您的问题与 httpClient 无关。

    从您的解释看来,您希望帖子的正文包含“ClassName”对象,但您实际发布的是“Informazioni”对象,并且该对象的唯一成员,如您的定义所示,是“idpaese”和“partitaivacf”。

    我猜你只是对你实际定义的数据和你期望的数据有点困惑。

    显示您的代码的 stackblitz 示例,以及您期望它如何工作与实际工作方式的详细描述,将帮助人们指导您找到更具体的解决方案。

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 2017-07-22
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多