0

I am trying to send a POST request with HttpClient Angular to a server. I have error in code:

file *.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!");
    });
}

file *.html

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

file json.php

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

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

run massage in console

{get: Array(0), post: Array(0)}
  get: []
  post: []
  __proto__: Object
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
ashe405
  • 59
  • 1
  • 9
  • Welcome to Stack Overflow! Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. But I think your question is still not answerable. **You** should [edit] your question now, to add missing details (see [mcve] ). Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Oct 22 '18 at 08:14
  • What is the error ? – Sunil Singh Oct 22 '18 at 08:45
  • cannot post postData to server Nothing data to server But im send postData.append('parentid','0'); – ashe405 Oct 22 '18 at 08:50

1 Answers1

1

You are reffering to the function postData instead of the variable it should be postData instead of this.postData:

 this.http.post(this.link, postData).subscribe(data =>{
   console.log(data);
 }, error => {
   console.log("Oooops!");
 });
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77