Angular 4 http post with php

Component or Service:

let data = {'title': 'foo',	'body': 'bar', 'userId': 1};
    this.http.post('http://localhost/angular4/angcrud/add_student_action.php', data)
      .subscribe(
        (res:Response) => {
          console.log(res.json());
        },
        err => {
          console.log("Error occured");
        }
      );

 

Php Script:

<?php


$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
//$res_ar = array("foo"=> $_REQUEST['body']);


echo json_encode($request);

?>


form #studentForm = "ngForm" (ngSubmit)="addStudentRecord(studentForm.value)">
  First name:<br>
  <input type="text" name="firstname" value="Mickey" ngModel>
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse" ngModel>
  <br><br>
  <input type="submit" value="Submit">
</form><

 

3 Replies to “Angular 4 http post with php”

Leave a Reply

Your email address will not be published. Required fields are marked *