Post 在 ionic 3 中,不能 post

Post in ionic 3, cannot post

我正在尝试使用 HttpClient Angular 向服务器发送 POST 请求。我的代码有错误:

文件*.ts

import {  HttpClient } from '@angular/common/http';

constructor(public http: HttpClient){}

public link ='http://opencart-ir.com/test/json.php';
public postData(){
  let postData = new FormData();
  postData.append('parentid','0');
  this.http.post(this.link,this.postData)
  .subscribe(data =>{
    console.log(data);
  }, error => {
    console.log("Oooops!");
    });
}

文件*.html

<button (click)="postData()">Post</button>

文件json.php

<?php
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');

  $data= array(
    'get' => $_GET,
    'post' => $_POST
  );
  echo json_encode($data);
?>

运行 在控制台中进行按摩

{get: Array(0), post: Array(0)}
  get: []
  post: []
  __proto__: Object

您指的是函数 post Data 而不是变量,它应该是 postData 而不是 this.postData:

 this.http.post(this.link, postData).subscribe(data =>{
   console.log(data);
 }, error => {
   console.log("Oooops!");
 });