Angular 5 – HttpClient Post没有发布

我有一个httpClient帖子,通过服务,没有给我任何错误,但它也没有将数据发布到数据库.

这是代码:

dataService.ts

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

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

@Injectable()
export class DataService {

  constructor(private http: HttpClient) {}

  insertData() {
    const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
    return this.http.post('http://myurl/index.php', body, httpOptions);
  }

}

app.component.ts

import { Component } from '@angular/core';
import { DataService } from './services/data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  constructor(private dataService: DataService) {}

  myInsert() {
    if (this.dataService.insertData()) {
      alert('Data Inserted Successfully');
    }

  }

}

最后是app.component.html

<div (click)="myInsert()">Click Me</div>

我已经检查了chrome网络上的帖子,但没有显示任何内容.
我怎样才能解决这个问题?

最佳答案
需要观察观察者提出请求.

myInsert() {
  this.dataService.insertData().subscribe(
    response => console.log(response),
    err => console.log(err)
  );
}

转载注明原文:Angular 5 – HttpClient Post没有发布 - 代码日志