Angular 4 http post with php

Component or Service:

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


Php Script:


$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>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse" ngModel>
  <input type="submit" value="Submit">


4 Replies to “Angular 4 http post with php”

Leave a Reply

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