【问题标题】:Angular 8 HttpClient Call Post [duplicate]Angular 8 HttpClient调用发布[重复]
【发布时间】:2019-10-05 16:47:52
【问题描述】:

我应该在 Angular 中使用 httpclient 模块进行 post 调用,但在这种情况下调用没有执行,应该怎么办?

app.component.html:

<div class="col-xs-12">
   <button class="btn btn-primary" (click)="getData()">Chiamata rest</button>
</div>

app.component.ts:

import { Injectable,Component, OnInit ,DoCheck} from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';


const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

getData(){
    const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
    return this.http.post('http://localhost:8787/utente/nuovoutente', body, httpOptions);
  }

【问题讨论】:

    标签: angular post httpclient


    【解决方案1】:

    在实际解决问题之前,您需要做一些事情。您应该将调用 api 的部分移动到角度服务。

    如果您希望它执行,您必须订阅该调用。请参阅HttpClient 文档。

    如果您只想在组件中调用 API,请将代码更改为,

    getData(){
        const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
        this.http.post('http://localhost:8787/utente/nuovoutente', body, httpOptions).subscribe((data) =>{
           console.log(data);
        });
    };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2018-07-05
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多