Angular httpclient.post无法创建正确的正文

时间:2019-01-10 17:22:09

标签: angular post httpclient

我有一个角度服务,必须通过邮寄发送一些数据。

我必须发送的课程是:

   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"
  }

很明显,当我打开模板的页面时,抛出一个错误,即模板无法读取未定义的notifyazioni对象的属性。可能与生成的物体有关吗?还是可能是我忽略的另一个问题?

预先感谢

1 个答案:

答案 0 :(得分:0)

您的问题似乎与httpClient无关。

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

我猜您对实际定义的数据和期望的数据有些困惑。

一个显示代码的stackblitz示例,以及有关代码预期工作方式与实际工作方式的详细说明,将帮助人们将您引向更具体的解决方案。

相关问题